In PHP, stripos() is a very common function that is used to find another substring in case-insensitive in one string. Although the basic usage of this function is very simple, its third parameter, $offset , is often ignored or misused.
This article will introduce in detail the role of the $offset parameter, usage scenarios, and how to use it to optimize the efficiency of string search.
The syntax is as follows:
int|false stripos(string $haystack, string $needle, int $offset = 0)
$haystack : The string to search for.
$needle : The substring to be found.
$offset (optional): The location to start the search.
Return value: If found, return the first occurrence position (starting from 0); if not found, return false .
Note: stripos() is case-sensitive, while strpos() is case-sensitive.
$offset specifies where to start looking for substrings, which starts from the beginning of the string (i.e., position 0).
This is especially useful in the following scenarios:
For example, suppose we want to find a keyword that appears the second time from a paragraph of text:
$text = "Welcome to visit m66.net,Our website is https://m66.net/page。";
$firstPos = stripos($text, "m66.net");
if ($firstPos !== false) {
// Start looking for the second time after the first appearance
$secondPos = stripos($text, "m66.net", $firstPos + 1);
echo "The second time it appears is:$secondPos";
}
Output:
The second time it appears is:22
By setting $offset = $firstPos + 1 we avoid duplicate matches that first appears.
When looking for multiple identical substrings in some long strings, you can combine loops and $offset for "paginated searches":
$content = "Click on the link https://m66.net/a,Click again https://m66.net/b,Last visit https://m66.net/c";
$needle = "https://m66.net/";
$offset = 0;
while (($pos = stripos($content, $needle, $offset)) !== false) {
echo "Find the location:$pos\n";
$offset = $pos + strlen($needle); // Continue to search later
}
Output:
Find the location:6
Find the location:26
Find the location:47
This method is particularly suitable for positioning multiple identical keywords in data analysis, text processing, and log analysis.
When you already know that a certain part of the text is irrelevant, or you are only interested in the second half, using $offset can significantly improve performance and avoid unnecessary matching attempts, especially when dealing with long strings:
$largeText = file_get_contents('large_text.txt');
$offset = strpos($largeText, 'Start the text');
if ($offset !== false) {
$startSearchFrom = $offset + strlen('Start the text');
$pos = stripos($largeText, 'Keywords', $startSearchFrom);
echo "Keywords出现的位置:$pos";
}
Compared to searching from scratch, skipping searches that are unrelated areas is more efficient and resource-saving.
Negative offset : Although negative values can be used, they will lead to unexpected behavior in most scenarios. Unless you really understand the purpose, it is not recommended to use it like this.
Ignoring $offset causes duplicate matches : If you need to find multiple occurrences of targets, be sure to set $offset , otherwise it will always return to the first occurrence location.
The $offset parameter in the stripos() function is a powerful but often overlooked feature. Using $offset rationally can make your code more efficient and flexible. Whether it is looking for substrings that appear multiple times or skipping processed content, $offset comes in handy.
Next time you write search-related logic, you might as well stop and think about it: Should I add $offset?