Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. strpos() / strrpos() / mb_strpos()

strpos() / strrpos() / mb_strpos() Since: PHP 4(2000)

Searches a string for a specified substring and returns the position where it was found. If the string contains multibyte characters, use mb_strpos() instead.

Syntax

// Searches from the beginning and returns the position of the first match.
strpos($haystack, $needle, $offset);

// Searches from the end and returns the position of the last match.
strrpos($haystack, $needle, $offset);

// Searches from the beginning with multibyte character support.
mb_strpos($haystack, $needle, $offset, $encoding);

// Returns the portion of the string from the first match onward.
strstr($haystack, $needle, $before_needle);

// Searches case-insensitively.
stristr($haystack, $needle, $before_needle);

Function List

FunctionDescription
strpos($haystack, $needle, $offset)Searches from the beginning and returns the position of the first match. Returns false if not found. The third argument sets the starting offset and can be omitted.
strrpos($haystack, $needle, $offset)Searches from the end and returns the position of the last match. Returns false if not found.
mb_strpos($haystack, $needle, $offset, $encoding)A multibyte-aware version of strpos(). Use this for strings that contain non-ASCII characters such as Japanese.
strstr($haystack, $needle, $before_needle)Returns the portion of the string from the first match onward. If the third argument is true, returns the portion before the match instead.
stristr($haystack, $needle, $before_needle)A case-insensitive version of strstr().

Return Value

strpos(), strrpos(), and mb_strpos() return the position as an integer, or false if the substring is not found. strstr() and stristr() return the matched portion of the string, or false if not found.

Sample Code

<?php
$str = "東京都渋谷区神南1丁目";

// Search with strpos(). Returns a byte offset for multibyte strings.
echo strpos($str, "渋谷"); // Outputs the UTF-8 byte offset '9'.

// Use mb_strpos() to get the correct character position.
echo mb_strpos($str, "渋谷", 0, "UTF-8"); // Outputs '3'.

// Returns false if the substring is not found.
var_dump(mb_strpos($str, "大阪", 0, "UTF-8")); // Outputs 'bool(false)'.

// Use strrpos() to find the position of the last match.
$path = "/var/www/html/index.php";
echo strrpos($path, "/"); // Outputs '13'.

// Use strstr() to get the portion of the string from the match onward.
$email = "user@example.com";
echo strstr($email, "@"); // Outputs '@example.com'.

// Pass true as the third argument to get the portion before the match.
echo strstr($email, "@", true); // Outputs 'user'.

// stristr() is case-insensitive.
echo stristr("Hello World", "hello"); // Outputs 'Hello World'.

// Use strict comparison when checking the return value of strpos().
if (strpos("abcdef", "abc") !== false) {
	echo "Found."; // Works correctly even when the match is at position 0.
}

Notes

strpos() searches a string for a substring and returns the position of the first match. It returns false when the substring is not found, but returns 0 when found at the very beginning — so using == for comparison will not work correctly. Always use !== false for strict comparison.

For strings containing multibyte characters, strpos() returns a byte offset rather than a character position. Use mb_strpos() to get the correct character position.

If you simply need to check whether a string contains a substring, you can use str_contains() in PHP 8.0 or later. To extract a portion of a string starting from a specific character — such as getting the domain from an email address — strstr() is a convenient choice. To replace substrings, use str_replace().

If you find any errors or copyright issues, please .