PHP Reference · String Functions
PHP stripos() function
Reference for PHP stripos(): find the first occurrence of a substring ignoring case, the 0 versus false pitfall, offsets and multibyte limits.
stripos() works exactly like strpos() but ignores differences between upper and lower case. It returns the zero-based position of the first match, or false if the needle is not present.
As with strpos(), a match at the start returns 0, so always compare the result with false using === or !==.
<?php var_dump(stripos("Hello World", "WORLD")); // int(6) var_dump(stripos("Hello World", "xyz")); // bool(false)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
stripos(string $haystack, string $needle, int $offset = 0): int|false
| Name | Description |
|---|---|
$haystack |
The string to search in. |
$needle |
The substring to find, compared without regard to case. |
$offset |
Where to start. A negative value counts from the end. The result is still relative to the start of $haystack. |
Return value: the position of the first match as an int, or false.
Errors: a ValueError is thrown if $offset is outside the string.
Usage notes
- Compare with
false.if (stripos($s, "a"))fails when the match is at position 0. - ASCII-only case folding. Since PHP 8.2 case conversion in
stripos()is locale-independent and affects only ASCII letters. For accented or non-Latin text usemb_stripos(). - Byte positions. The result counts bytes, like
strpos(). - Just a yes/no? Lower-case both strings and use
str_contains(), or comparestripos(...) !== false. - Last occurrence. There is a matching
strripos()for searching from the end without regard to case. - Slightly slower than
strpos()because it normalizes case; usestrpos()when case matters or is known.
Related
strpos(),strrpos(),str_contains(),strtolower()- Lessons: Strings, String functions