Definition and Usage
- PHP Version
- 5+
The stripos() function for strings searches for the specified substring in a string and returns the index of its first occurrence.
This function performs a case-insensitive search.
If the specified substring is not found, it returns false.
This function is useful for finding the position of a substring in a string or for checking whether it exists, in a case-insensitive manner.
Features
- The comparison is case-insensitive.
- This function does not support multi-byte strings such as Japanese, Korean, or Chinese.
Basic Example
$str = 'Hello, World!';
$substring = 'world'; // Case-insensitive search
$pos = stripos($str, $substring);
if ($pos !== false) {
echo "Found at position: $pos";
} else {
echo 'Not found.';
}
// Output: Found at position: 7
/*
* Note:
* String indexes in PHP start at 0.
* The index of the first character is 0, and the second character is 1.
*/
Useful Tips
- For multi-byte strings such as Japanese, Korean, or Chinese, use the
mb_stripos()function instead. - If you need a case-sensitive search, use the
strpos()function.
Syntax
stripos(string $haystack, string $needle, int $offset = 0): int|false
Parameters
$haystack |
The input string to be searched. |
|---|---|
$needle |
The substring to search for.
The comparison is case-insensitive. |
$offset |
Optional. The zero-based index at which to start the search.
|
Return Values
Returns the position (index) of the first occurrence of the specified substring ($needle) within the input string ($haystack).
Note:
String positions (indexes) start at 0, not 1.
- Returns
falseif the specified substring ($needle) is not found. - Returns
falseif the given substring ($needle) is an empty string ('').
Important:
The strpos() function triggers a warning in PHP versions earlier than 8.0 when the given substring ($needle) is an empty string (''), unlike stripos().
$haystack = 'Hello, World!';
var_dump(stripos($haystack, '')); // bool(false)
var_dump(strpos($haystack, '')); // Warning: strpos(): Empty needle in
Changelog
| Version | Description |
|---|---|
| 8.2.0 | Prior to PHP 8.2.0, the results of case-insensitive comparisons for certain non-ASCII characters (e.g., Japanese, Korean, Chinese) could vary depending on the setlocale() setting. Starting with PHP 8.2.0, only ASCII characters are compared in a case-insensitive manner; all other characters are compared at the byte level. |
| 8.0.0 | Empty strings ('') are now allowed for the $needle parameter. |
| 8.0.0 | Passing an integer (int) to the $needle parameter is no longer supported. |
| 7.3.0 | Passing an integer (int) to the $needle parameter has been deprecated. |
| 7.1.0 | Negative values are now allowed for the $offset parameter. |
Things to Keep in Mind
String indexes start at 0.
The first character has an index of 0, and the second character has an index of 1.
0.
$newstring = 'abcdef ghijk';
$pos = stripos($newstring, 'a');
var_dump($pos); // int(0)
Be careful when using this function's return value in a Boolean context.
To properly test the return value, always use the === operator.
If the specified substring ($needle) is found at the very beginning of the string ($haystack), the function returns 0.
However, because 0 is loosely evaluated as false in PHP, using == may lead to incorrect results. For strict comparison, use ===.
=== operator when testing the return value.
$str = 'Hello, World!';
$substring = 'Hello';
$pos = stripos($str, $substring);
var_dump($pos); // int(0)
if ($pos === false) {
echo "The string 'Hello' was not found.";
} else {
echo "The string 'Hello' is included in the text.";
}
// Output: The string 'Hello' is included in the text.
Practical Examples
The stripos() function is useful for finding the position of a substring in a string. Its main feature is that it performs a case-insensitive search, allowing you to locate substrings without worrying about letter case.
Searching User Input
$user_input = $_GET['search_query'];
$content = 'This is a sample text for searching.';
if (stripos($content, $user_input) !== false) {
echo 'The search term was found.';
} else {
echo 'The search term was not found.';
}
Case-Insensitive Substring Check
$string = 'The school bell rings, time to gather!';
$search_term = 'Bell';
if (stripos($string, $search_term) !== false) {
echo 'The search term was found.';
} else {
echo 'The search term was not found.';
}
// Output: The search term was found.
Checking if a String Starts with a Specific Word
$string = 'Hello, world!';
$prefix = 'hello';
if (stripos($string, $prefix) === 0) {
echo "The string starts with '$prefix'.";
} else {
echo "The string does not start with '$prefix'.";
}
// Output: The string starts with 'hello'.
References
See also
- PHP mb_stripos() Function – Multibyte-Safe Alternative to stripos()
- PHP strpos() Function – Find First Substring Index
- PHP mb_strpos() Function – Multibyte-Safe Alternative to strpos()
- PHP str_contains() Function – Check if a String Contains a Substring (Case-Sensitive)
- PHP substr() Function – Slice Strings by Position and Length
- PHP mb_substr() Function – Multibyte-Safe Alternative to substr()
- PHP preg_match() Function – Check if a String Matches a Regular Expression