PHP Search String | Searching Strings for Substrings in PHP

PHP Search String
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:-

.
Important Note:
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.

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.

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.

.
Important Note:
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.

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:-

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:

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:-

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.

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:

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:

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:

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:

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:

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:

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:

.
Important Note:
strlen() the function is used for returns character length in the string.
empty() the function is for tells whether a variable is empty
substr() the function is used for returns extracted part of a string
strstr() 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 string

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

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.

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)

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

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.