PHP searchstring | How to Searching Strings in string PHP?

Summary: In this article, you will learn about PHP searchsring, and How to search a string for a particular chunk of text. Let’s understand the searching string in PHP with an example.

Overview of PHP Searchstring

PHP searchstring plays important role, When writing PHP scripts, sometimes you often need to search a string for a particular or selected chunk of text. For Instance, you may be writing a search engine to search through pages of content, otherwise, you might want to know if a URL or email address contains a certain domain name.

PHP provides you many useful functions for searching strings. let’s understand In this article:

  • strpos() and strrpos() used in PHP for finding the position of some text in a string
  • strstr() is used in PHP for finding out whether some text is in a string
  • substr_count() used in PHP for finding out how many times some text appears in a string

Finding the position of some text in a string: strpos() and strrpos()

strpos() takes the same two arguments as strstr(). However, rather than returning a portion of the string, furthermore, it returns the index of the first character of the matched text in the string:

Output: 20

In the above following code segment, strpos() finds the text ‘scan’ in the target string, so it returns 20.

.
Warning: Remember that the character index positions in strings start from 0, not 1.

Furthermore, be careful when you are using strpos() to check for the existence of a match. The following code incorrectly displays “Not found”, because strpos() returns 0, which is equivalent to false in PHP:

Output: it’s found

To fix this, make sure you test explicitly for false by using the === or !== operator. furthermore, the following code correctly displays “Found”:

You can pass a third(3rd) argument to strpos() to specify the index position from which to begin the search:

The PHP strrpos() function is very similar to strpos() function. furthermore, the only difference is that it finds the last match in the string instead of the first:

.
Warning: As with strstr(), the strpos() and strrpos() functions are case sensitive. Their case-insensitive equivalents are stripos() and strripos() respectively.

PHP searchstring: Simple text searching with strstr()

PHP strstr() function used to search string, and a chunk of text to search for. If the text was found, then it will return the portion of the string from the first character of the match up to the end of the string:

Output: scan!

If the text was not found then strstr() will returns false. furthermore, you can use this fact to determine or identify if the text chunk was in the string or not:

Output: Text not found

The counting matches with substr_count()

You can use PHP’s substr_count() function to find the number of times a chunk of text appears in the target string:

As with strpos() and strrpos(),therefore, you can pass an optional third(3rd) argument: the index position to begin the search. the following example:

You can also pass an optional fourth(4th) argument: therefore, the number of characters after the offset position in which to search for the text. such as for example:

Conclusion:

In this article, you’ve learned how to use PHP searchsring. such as PHP strstr() function for finding out whether a chunk of text exists in a string and PHP strpos() and strrpos() function for locating text in a string; and also substr_count() for finding out the number of matches in a string. I hope you will enjoy it!