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:
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:
$myString = 'Hello, there!';
echo strstr( $myString, 'llo' ); // Displays "llo, there!"
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:
$myString = 'Hello, there!';
if ( strstr( $myString, 'Goodbye' ) ) {
echo "Text found";
} else {
echo "Text not found";
}
The above code displays:
Text not found
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:
$myString = 'Hello, there!';
echo strpos( $myString, 'llo' ); // Displays "2"
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:
$myString = 'Hello, there!';
if ( strpos( $myString, 'Hello' ) ) {
echo "Found";
} else {
echo "Not found";
}
To fix this, make sure you test explicitly for false
by using the ===
or !==
operator. The following code correctly displays “Found”:
$myString = 'Hello, there!';
if ( strpos( $myString, 'Hello' ) !== false ) {
echo "Found";
} else {
echo "Not found";
}
You can pass a third argument to strpos()
to specify the index position from which to begin the search:
$myString = 'Hello, there!';
echo strpos( $myString, 'e' ) . '<br />'; // Displays "1"
echo strpos( $myString, 'e', 2 ) . '<br />'; // Displays "9"
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:
$myString = 'Hello, there!';
echo strpos( $myString, 'e' ) . "<br />"; // Displays "1"
echo strrpos( $myString, 'e' ) . "<br />"; // Displays "11"
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:
$myString = 'Short stories';
echo substr_count( $myString, 'or' ); // Displays "2"
As with strpos()
and strrpos()
, you can pass an optional third argument: the index position to begin the search. For example:
$myString = 'Short stories';
echo substr_count( $myString, 'or', 6 ); // Displays "1"
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:
echo substr_count( $myString, 'or', 0, 10 ) . '<br />'; // Displays "2"
echo substr_count( $myString, 'or', 0, 5 ) . '<br />'; // Displays "1"
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!
achandia says
This is my case: Mysql Column called TAG Containing this information:
row1 Note: bla bla bla1 – Tag: bla bla bla1
row2 Note: bla bla bla2 – Tag: bla bla bla2
row3 Note: bla bla bla3 – Tag: bla bla bla3
row4 Tag: bla bla bla4
row5 Note: bla bla bla4
So I want to do a query with php that displays the all rows, but from column TAG display the info containing only the string corresponding to “Note: bla bla blaX”
so in the row 4 shouldn’t show anything but at the others rows should show the corresponding Note.
I hope it is not too difficult.
Thanks for your help.
matt says
Hi achandia,
You need to use a regular expression for this, eg:
You’ll probably want to trim any whitespace from the end of the match too.
It’s a pretty nasty data format – you’d be better off having your notes and tags in separate columns in the DB, if possible.
achandia says
Thanks for your answer, the problem is I can not use a different colum, that’s why I’m looking for a solution like this, actually to make it easier may be I can use tags, I mean something like thiis:
Row Stuff Info
row1 First Stuff <N>bla bla bla1</N><T>bla bla bla1</T>
row2 Second Stuff <N>bla bla bla2</N><T>bla bla bla2</T>
row3 Third Stuff <N>bla bla bla3</N><T>bla bla bla3</T>
row4 Fourth Stuff <T>bla bla bla1</T>
row5 Fifth Stuff <N>bla bla bla1</N>
And make the regular expression accordingly
I will try your solution and I let you know how it goes.
Thanks again.
[Edited by achandia on 11-Nov-10 02:29]
achandia says
Thanks a lot Matt, following your code I could solve the problem, this is the code I finally used:
matt says
Yes, that is a better data format. You want to use delimiters that will never be seen in the data.
Glad you solved it 🙂
Cheers,
Matt
Mostafa Atef says
thanks
Rick says
A very useful article. Thank you. I found that strstr() also takes a true or false argument.
If set to true, it will return the part of the string before the first occurrence of the match:
$string = ‘Hello, there!’;
$outstring = strstr($string, ‘llo’, true);
echo $outstring; // Displays ‘He’