This content originally appeared on DEV Community and was authored by hrishikesh1990
In this short tutorial, we look at how to check if a string contains a substring in PHP. We also take a look at the other relevant string manipulation methods in PHP.
This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts. However, in case you are here only for the solution use this link.
Table of Content
- Why check if a PHP string contains a substring?
- Using
str_contains
- Using
stripos
,strpos
andstrrpos
- Limitations and Caveats
Why check if a PHP sting contains a substring?
Similar to other programming languages, it is a common practice to check if a string contains a substring. This could be for numerous reasons, subsequently, they are a few inbuilt functions that we could use to check if a PHP string contains a substring. The PHP string contains (str_contains
) is one of the most commonly used methods, however, the methods you chose would large me decided based on your use cases.
If you are looking to just check if a PHP string contains a substring your can used the str_contains
function. if you are looking to check if a PHP sting contains a substring and returns its index you could use the stripos
and strpos
methods. With that out of the way let us look at all the methods in depth.
Using str_contains
:
The str_contains
is a new function that was introduced in PHP 8. The str_contains
method is used to determine if a PHP string contains a substring. The function checks the string and returns a boolean true
in case it exists and false
otherwise. However, keep in mind that str_contains
is case sensitive.
Syntax of PHP str_contians
:
str_contains(string, substring)
Parameters:
string
- Required, the original PHP string that you are looking to search
substring
- Required, the substring that you are looking to check if the PHP string contains
Code & Explanation:
<?php
$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'Hire')) {
echo "PHP string contains 'Hire'";
} else {
echo "PHP string does not contain 'Hire";
}
?>
//Output: "PHP string contains 'Hire'"
As the str_contains
returns true
or false
by placing it inside an if.
however, the str_contains
methods is case sensitive and return false
in case the PHP string contains the substring in a different case. Here is an example.
<?php
$string = "Hire the top 1% freelance developers"
if (str_contains($string, 'hire')) {
echo "PHP string contains 'Hire'";
} else {
echo "PHP string does not contain 'Hire";
}
?>
//Output: PHP string does not contain 'Hire"
However, in case you aren't aware of the case of the substring you could use the strtolower()
function.
Using stripos
, strpos
and strrpos
In case you are looking to check if a PHP string contains a substring, and to find its index these methods are your solutions. The stripos
method checks if a PHP string contains a substring, and return the index of the first occurrence of the substring. However, the stripos
method is case-insensitive, and if you are looking to case-sensitively check if a PHP string contains a substring you can use the strpos
methods. And lastly, strrpos
unlike the previous two, this method returns the last occurrence of the substring in the original PHP string.
Syntax of stripos
, strpos
and strrpos
//Syntax of stripos
stripos(string, substring, start)
//Syntax of strpos
strpos(string, substring, start)
//Syntac of strrpos
strrpos(string, substring, start)
Parameters:
string
- Required, the original PHP string that you are looking to search
substring
- Required, the substring that you are looking to check if the PHP string contains
start
- Optional, specific where the search for the substring should begin
Returns:
When used to check if a PHP string contains a substring all the three methods return an index between 0
to n-1
returns false
if the substring is not found.
However, given that 0 is also a falsy value remember to use the identical or not identical operator ===
or ! ==
and not the equal to operator ==
as 0
also would be considered as false
. You can refer to the code below.
Code & Explanation:
<?php
$string = "Hire the top 1% freelance developers"
$substring_index = stripos($string, "Top");
if($substring_index !== false) {
echo "'Top' is a substring!";
}
if($substring_index !== false) {
echo "'top' is a substring!";
}
// Output = 'Top' is a substring!
// Output = 'top' is a substring!
?>
As stripos
is case insensitive, it does not return a false
when used to check if the PHP string contains the 'Top'
substring. However, this is not the case when strpos
or strrpos
are used. Now let's look at a case where the index is 0
.
<?php
$string = "Hire the top 1% freelance developers"
$substring_index = stripos($string, "Top");
if($substring_index != false) {
echo "'Hire' is a substring";
} else {
echo "'Hire' is not a substring";
}
//Output = "'Hire' is not a substring"
if($substring_index !== false) {
echo "'Hire' is a substring";
} else {
echo "'Hire' is not a substring";
}
//Output = "'Hire' is a substring"
?>
As you can see the initial ! =
operator considers 0 as false
this is why it is important to your the identical operators.
Limitations and Caveats
- Which using the latter functions to check if PHP string contains a value remember to use the identical operators
- Not all methods are case-insensitive, ensure to use the
strtolower()
method
This content originally appeared on DEV Community and was authored by hrishikesh1990
hrishikesh1990 | Sciencx (2021-06-03T11:58:52+00:00) How to check if a PHP string contains a substring?. Retrieved from https://www.scien.cx/2021/06/03/how-to-check-if-a-php-string-contains-a-substring/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.