Php Search String
Oct 07, 2020 A String is a sequence of characters, which is used either as a literal constant or as some kind of variable. A specific part of a string is known as substring. The PHP provides strpos function to check if a string contains a specific substring or not. The strpos function returns the position of the first occurrence of a substring in a string.
When writing PHP scripts, you often need to search a string for a particular chunk of text. For example, you might be writing a search engine to search through pages of content, or you might want to know if a URL or email address contains a certain domain name.
PHP gives you many useful functions for searching strings. In this article you look at:
- This is a PHP tutorial on how to check if a string contains a given substring. Does the string contain a certain word or a set of characters? A case insensitive check using the stristr function.
- . Will search string and replace all anchor tags with text (case insensitive). How it works:. Searches string for an anchor tag, checks to make sure it matches the criteria. Anchor search criteria:. 1.
- Oct 13, 2009 When writing PHP scripts, you often need to search a string for a particular chunk of text. For example, you might be writing a search engine to search through pages of content, or you might want to know if a URL or email address contains a certain domain name. PHP gives you many useful functions for searching strings. In this article you look at.
- Using PHP pregmatch to Search for Patterns in Strings in PHP The PHP pregmatch function is used to find a match within strings based on your specified pattern. It has two required parameters: the pattern and the string in which we want to find a match.
strstr()
for finding out whether some text is in a stringstrpos()
andstrrpos()
for finding the position of some text in a stringsubstr_count()
for finding out how many times some text appears in a string
Simple text searching with strstr()
PHP’s strstr()
function simply takes a string to search, and a chunk of text to search for. If the text was found, it returns the portion of the string from the first character of the match up to the end of the string:
If the text wasn’t found then strstr()
returns false
. You can use this fact to determine if the text chunk was in the string or not:
The above code displays:
strstr()
is case sensitive — for example, 'hello'
Php Search String For String
won’t match'Hello'
. If you don’t care about matching case, use the case-insensitive version, stristr()
, instead.Finding the position of a match: strpos()
and strrpos()
strpos()
takes the same 2 arguments as strstr()
. However, rather than returning a portion of the string, it returns the index of the first character of the matched text in the string:
In the above code, strpos()
finds the text 'llo'
in the target string starting at the 3rd character, so it returns 2
. (Remember that character index positions in strings start from 0, not 1.)

Be careful when 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:

To fix this, make sure you test explicitly for false
by using the or !
operator. The following code correctly displays “Found”:
You can pass a third argument to strpos()
to specify the index position from which to begin the search:
The strrpos()
function is very similar to strpos()
. The only difference is that it finds the last match in the string instead of the first:
strstr()
, the strpos()
and Php Search String
strrpos()
functions are case sensitive. Their case-insensitive equivalents are stripos()
and strripos()
respectively.Php Search String For Values In Array
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()
, you can pass an optional third argument: the index position to begin the search. For example:
You can also pass an optional fourth argument: the number of characters after the offset position in which to search for the text. For example:
In this article you’ve looked at how to search strings in PHP. You’ve explored the strstr()
function for finding out whether a chunk of text exists in a string; strpos()
and strrpos()
for locating text in a string; and substr_count()
for finding out the number of matches in a string. Happy coding!
Php Search String For Pattern
Many browsers support the use of 'search strings', which are custom URLs containing a placeholder for your search term. The browser can swap out the placeholder with your search term and submit the URL, allowing Startpage searches to be performed even if the browser isn't explicitly supported by Startpage.
If you are using a browser that is not supported by Startpage but supports the use of search strings, you should be able to add Startpage using the following search string:
Type the URL above exactly as shown. The '%s' is the placeholder for your search strings. Some browsers may require the use of a different placeholder, such as '%term', in which case you can substitute that for '%s'.