This article will provide you with a few functions that serve one very commonly asked PHP question: how can I find if there's an occurrence of a string in another string. We'll provide just a couple of examples, although there's countless ways in which it can be accomplished.
stripos will find the numeric position of the last occurrence of needle in the haystack (case-insensitive) string (or last occurrence of a substring in a string). The Boolean result is intended to work with a case-insensitive 'search' although it's easy to alter for case-sensitivity with the use of strpos() .
So, to check for
$word = 'Aquarius' in
$string = "This is the age of Aquarius", we would use the following. It will return 'Match found'.
Writing a wrapper for a function is normally a little bizarre... except we've changed the requirements a few times for an application we worked on, and had to occasionally alter the way in which data was matched. The considerations at the bottom of the page might give you an indication of why we've altered a default function. The following is just an example:
The example usage returns
true. It will also return true for a partial... so searching for 'cat' will also return true if the word 'cats' is in the string (the functions will also match spaces so this is often a ways of navigating this issue).
$offset is specified, the search will start this number of characters counted from the beginning of the string. If the offset is negative, the search will start this number of characters counted from the end of the string.
PHP's preg_match searches a subject for a match to the regular expression given in pattern. While using a regular expression might cost you a few seconds of processing power over the course of its lifetime, it sometimes yields a better result - particularly if you don't want to match a partial.
Our example usage will return
true. Unlike the former function, this function won't return a partial match as true. The "i" after the pattern delimiter indicates a case-insensitive search. Remove for case-sensitivity.
Unlike the PHP functions above, there's no offset... although you could use other means to return a partial string.
- The str[i]pos() functions were what we traditionally used as a check, however they didn't provide a negative
$offsetargument until PHP 7 (earlier versions will throw an error). The function finds the position of the first occurrence of a substring in a string. If the
$offsetis negative, the search will start this number of characters counted from the end of the string, and search forwards (returning the integer value of the first occurrence).
- The strr[i]pos() (note the extra 'r') functions provided for a negative
$offsetargument prior to PHP 7. The function finds the position of the last occurrence of a substring in a string. If the value of the
$offsetis negative, search will instead start from that many characters from the end of the string, searching backwards (returning the integer value of the last occurrence).
strr[i]pos()uses a negative offset, the function need only match part of your
$needleto return true.
- If the offset is greater than the haystack the function will throw an error:
strrpos(): Offset is greater than the length of haystack string in * on line *. For this reason, we'll truncate the length of the $offset to the length of the string. That said, it alters expected behaviour so shouldn't be used in this way if it's going to cause problems.
These functions are some that have always bothered me... while it's usually only the $offset use that causes potential problems. Keep in mind that while we've used our functions to test for
false, the intention of all the string functions are to provide an index for the $string position in the $haystack.