Nadia S.
—How do I check if a string contains a specific word in PHP?
Since PHP 8, we can use the str_contains()
function to check if a substring is present within a given string. In this example, we check whether a string URL has the http://
protocol:
$url = "http://www.example.com/info/about-us"; $protocol = 'http://'; // check $url for the presence of the $protocol substring echo str_contains($url, $protocol);
The function returns 1
for true
:
1
You can use an if
statement to echo out a string literal of the result, for example:
if (str_contains($url, $protocol)) { echo 'true'; } else { echo 'false'; }
Output:
true
strpos()
functionVersions of PHP previous to PHP 8 use the less intuitive strpos()
function to determine if a string contains a certain substring.
The strpos()
function returns an integer of the substring’s starting position if the substring is present, for example:
$url = "http://www.example.com/info/about-us"; $protocol = 'http://'; $ans = strpos($url, $protocol); echo $ans;
The output tells us that $protocol
is present and begins at the first character of $url
:
// index value 0
In contrast to str_contains()
, you need to explicitly include a comparator in an if statement, such as $foo === false
. For example:
if ($ans === false) { echo "The string '$protocol' is NOT in '$url'"; } else { echo "The string '$protocol' is in '$url'"; }
Output:
The string 'http://' is in 'http://www.example.com/info/about-us'
Note that you must use the ===
comparator instead of ==
if there is any chance of the substring in question starting at index 0
or the value of 0
will be interpreted as equating to false
. So this example:
if ($ans == false) { echo "The string '$protocol' is NOT in '$url'"; } else { echo "The string '$protocol' is in '$url'"; }
Will return:
The string 'http://' is NOT in 'http://www.example.com/info/about-us'
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.