and like us on FB

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.

String Functions

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:

Example Usage

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).

If $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.

Preg_Match() Function

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.

Considerations

  • The str[i]pos() functions were what we traditionally used as a check, however they didn’t provide a negative $offset argument 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 $offset is 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 $offset argument prior to PHP 7. The function finds the position of the last occurrence of a substring in a string. If the value of the $offset is 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).
  • If strr[i]pos() uses a negative offset, the function need only match part of your $needle to 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 true or false, the intention of all the string functions are to provide an index for the $string position in the $haystack.

Download

Title: Find a String in a String with PHP
Description: Find an occurrence of a string in another string. A few sample functions. Far from best practice.
Download: PHP Code (V0.2) | Plugin Page

Shortt URL for this post: http://shor.tt/Zk5