PHP Search String | Searching Strings for Substrings in PHP

In this article, I will discuss the PHP search string, Sometimes in PHP, when you writing the script, you often need to search a string for a particular chunk of text. there are several functions that PHP provides for search one string within another. furthermore, some return the location of the found string.
(strrpos, strpos and some related) and other return parts of the original string. therefore, In PHP search string, if the string which you are searching. for is not found within original then The search functions return false. suppose that if you simply determine whether one string exists within another, then the most efficient option is strpos.
consider an example, you want to write a search engine to search through pages of content, or you want to know if an email address or URL contains a certain domain name. For PHP Search String, I will discuss all functions, look at:-
strstr() function used for finding out whether some text is in the string.
strrpos() and strpos() function used for finding the position of some text in a string.
substr_count() function is used for finding out how many times some text appears in a string.
PHP Search String: Simple text searching with strstr()
The strstr function simply takes a string to search and a chunk of text to search for. the first string
the argument for the second, if the second is found within the first, strstr returns the portion of the
original string starting from the first found occurrence to the end of the string.
|
1 2 3 4 |
$mystr = "PHP contains functions for these two approach."; // search for 'two' in $mystr $phpstr = strstr($mystr, 'two'); var_dump($phpstr); // string(13) "two approach." |
therefore, the strstr function returns the first ‘two’ it finds, along with the rest of the original string.
if you pass true as the third argument to strstr, the portion of the original string before the
found string is returned.
|
1 2 3 |
// pass true to return the part of $mystr before 'two' $phpstr = strstr($mystr, 'two', true); var_dump($phpstr); // string(33) "PHP contains functions for these " |
now this time the strstr the function returns everything before the first ‘two’ in the string.
PHP also provides the other function such as the stristr function, which also works exactly
the same strstr except that it performs a case-insensitive search.
strstr() is a case sensitive — for example, “welcome” won’t match “Welcome”. therefore, If you don’t care about the matching cases, then use the case-insensitive version,
stristr()instead.To find the position of a match: use strpos() and strrpos()
How to use strpos() Function?:
Here, The PHP strpos function searches its first-string argument for its second and returns the zero-based
index location of the first match within the string, or false if it is not found. let’s see the
given following example.
|
1 2 3 4 5 6 |
// the string to use for searches $mystr = "Look up at the stars and not down your feet."; // search for the first occurrence of 'stars' within $mystr $pos = strpos($mystr, 'stars'); // display type and value of $pos var_dump($pos); // int(9) |
as the following is given above example the result using var_dump above, a typical examination of the return value for strpos is performed as follows, let’s see the given below:-
|
1 2 3 4 5 6 |
// How to inspect strpos return value ($pos) if ( $pos !== false ) { // if search string found echo "found it at location $pos"; } else { echo "not found."; } |
therefore, here we have a tendency to guaranteed to use the === or ! operators to check the strpos
function’s come back worth to false. because the following script, if the substring is found at
the beginning of the string, strpos can come back zero, that the == or != operators would convert to false.
You can specify the associate offset to start the search a nominative range of characters from the beginning of the string because the example demonstrates:
|
1 2 3 4 |
/* strpos arguments: * subject string (your feet), search string (stars), offset (optional) */ // start search for 'sta' from character 10 in $mystr $pos = strpos($mystr, 'sta', 10); // 15 |
therefore, when starting the search from character 10, the result is 15, so, the index location of the start of the word stars.
How to use strrpos() Function?:
The strrpos function used for finds the position of the last occurrence of a substring in a string,
let’s see the following example, how to find the position of the last occurrence of a substring:-
|
1 2 3 4 5 |
// string to use for searches $mystr = "PHP contains functions for these two approach."; // find location of last occurrence of 'func' in $mystr $pos = strrpos($mystr, 'func'); // 13 |
furthermore, in PHP the strrpos function also provides an optional offset parameter, which can be
either positive or negative. in that case, if the offset is positive, that number of characters
at the beginning of the string will be excluded from the search.
let’s consider the following example.
|
1 2 3 |
// search from right for 'PHP' excluding first 3 characters $pos = strrpos($mystr, 'PHP', 3); var_dump($pos); // bool(false) |
as the on top of, the result’s false since ‘PHP’ isn’t found as a result of once the search excludes
the primary 3 characters. what is more, therein case, if the offset is negative, that several characters
at the top of the string are excluded from the search. so, here we tend to demonstrate
with the 2 searches specifying a negative offset:
|
1 2 3 4 5 |
// search from right for 'app' excluding last 5 characters $pos = strrpos($mystr, 'app', -5); // int(37) // search from right excluding last 10 characters $pos = strrpos($mystr, 'app', -10); // bool(false) |
therefore, as per the result above is false since ‘app’ is not found when the search excludes the
last 10 characters.
here, you can notice that the return value of the strrpos function gives the location from the start
of the string, even though the search commences from the right.
PHP Search String: stripos() and strripos() Function
As the above function which has described, you can see that the strpos and strrpos functions perform
case-sensitive searches. furthermore, PHP provides stripos and strripos functions to perform case-sensitive searches. thus we can say that they work just like their case-sensitive equivalents, let’s see as
the demonstrates example:
|
1 2 3 4 5 6 7 8 |
// example string to use for searches $mystr = "PHP contains functions for these two approach."; // do case-insensitive search for 'PHP' $pos = stripos($mystr, 'PHP'); // int(0) // do case-insensitive search from right for 'fun' $pos = strripos($mystr, 'fun'); // int(30) |
as above the case-insensitive search for ‘PHP’ results in 0, indicating it was found at the beginning of the string, we are searching in. furthermore, for The case-insensitive search for ‘fun’ from the right (using strripos function), as a result, it finds at location 30.
How to use strrchr() Function?
The strrchr function searches the first string argument from the right for the character, here, we specify in the second argument. thus the function returns the portion of the string from the location of the found instance
of that character to the end of the string:
|
1 2 3 4 5 |
// example string $mystr = "PHP contains functions for these two approach."; // search from right for 'w' in $mystr $nextstr = strstr($str, 'w'); var_dump($nextstr); // string(12) "stack." |
here notice that unlike the strstr, because if the 2nd argument consists of multiple characters, only the first is used, let’s see the example:
|
1 2 3 |
// test with multi-character second argument $nextstr = strrchr($mystr, 'for'); var_dump($nextstr); // string(4) "functions" |
The Counting matches with substr_count() Function
In PHP, you can use substr_count() function to find the number of times a chunk of text appears in the target string:
|
1 2 |
$myStr = 'school playground'; echo substr_count( $myStr, 'or' ); |
As with strpos() and strrpos(), you can pass an optional third argument, therefore, the index position to begin the search, let’s see for example:
|
1 2 |
$myStr = 'school playground'; echo substr_count( $myStr, 'or', 6 ); |
here, you can also pass the optional fourth argument, furthermore, the number of characters after the offset position in which to search for the text, let’s see the example:
|
1 2 |
echo substr_count( $myStr, 'or', 0, 9 ) . '<br />'; echo substr_count( $myStr, 'or', 0, 6 ) . '<br />'; |
strlen() the function is used for returns character length in the string.empty() the function is for tells whether a variable is emptysubstr() the function is used for returns extracted part of a stringstrstr() the function is used to tells whether the search text is within the string.strpos() and strrpos() functions are used to return the index position of the first and last occurrence of the search text, respectively.substr_count() the function is used for tells how many times the search text occurs within the stringPHP Search String: Question & Answer
Find how many characters in a string?
The length of a string is the number of characters in a string, let’s see the example find how many characters in a string, let’s consider a string of “Management College” has a length of 18, with 17 characters for letters
and 1 character for space.
Here we used PHP strlen function returns the length of the given string.
|
1 2 |
$length = strlen('Management College'); echo $length; |
How to check if a string is empty?
therefore, the empty the function is used to check whether the variable is empty, if the variable is empty then it returns true. variable consider empty if it has not been set, or if the variable contains a NULL value, an empty string (""), or a string of 0.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// When a variable contain 0 . $variable = 0; if (empty($variable)){ echo '$variable is empty'; } // When a variable contains NULL value. $variable = NULL; if (empty($variable)){ echo '$variable is empty'; } // When will be empty string (""). $variable = ''; if (empty($variable)){ echo '$variable is empty'; } // When not empty $variable = 'it's not empty'; if (empty($variable)){ echo '$variable is empty'; } else { echo '$variable is not empty'; } |
How to extract a part of a string?
therefore, to extract a part of the string we used substr to function, a substring is a part of a string. Let’s see in the example, "Management" is a substring of "Management School". here, The substr function returns the portion of a string specified by the start and length parameters.
Syntax : substr($string, $position, $length)
|
1 2 3 |
$string = 'Management School'; $substring = substr($string, 0, 10); echo $substring; |
as you have seen, in the above example prints Management on the screen. therefore, when you working with strings, then you need to mention the position of a character within a string. here, the code gets a substring that starts at the first character (position 0) and the length has ten characters. as a final result, its returns a value of "Management".
|
1 2 3 |
$string = 'Management School'; $substring = substr($string, 11); echo $substring; |
as per the above code segment prints School on the screen, the length is omitted, therefore the substr function extracted the substring until the end of the string after mentioned the position 11. furthermore, if the length not provided, then the substr function returns string part after the mentioned position.