PHP: Find out whether a string contains a word
You often face the requirement of determining whether a particular word occurs in a given string or not.
From PHP 8 onwards
Since PHP 8 (available since 26 November 2020) there is the handy str_contains function for this. The check is case-sensitive:
<?php
$string = '6€ off your first order with the web host netcup using the code WFU4-A79w';
if (str_contains($string, 'netcup')) {
// The following line IS executed.
echo "The word 'netcup' was found in the string.\n";
}
if (str_contains($string, 'NETCUP')) {
// The following line is NOT executed.
echo "The word 'NETCUP' was found in the string.\n";
}
?>
If case should not matter when searching for the word, you can convert the whole string to upper case before the check. Use the strtoupper function for that:
<?php
$string = '6€ off your first order with the web host netcup using the code WFU4-A79w';
if (str_contains(strtoupper($string), 'NETCUP')) {
// The following line IS executed.
echo "The word 'NETCUP' was found in the string (regardless of case).\n";
}
?>
Incidentally, you can read up on the reasoning behind introducing the str_contains function here.
Before PHP 8
If you are running an older version of PHP you have to fall back on the strpos or the stripos function. The strpos function respects the case of the word you are looking for (case-sensitive), whereas for stripos case does not matter (case-insensitive).
<?php
$string = '6€ off your first order with the web host netcup using the code WFU4-A79w';
if (strpos($string, 'netcup') !== false) {
// The following line IS executed.
echo "The word 'netcup' was found in the string.\n";
}
if (strpos($string, 'NETCUP') !== false) {
// The following line is NOT executed.
echo "The word 'NETCUP' was found in the string.\n";
}
?>
Using the !== comparison operator here is especially important. strpos either returns the offset at which it found the word in the string — that is a value of type int — or it returns the boolean false if the word was not found.
If you use any comparison operator other than !==, PHP’s automatic type conversion will inevitably cause problems (type juggling).
Here is just one example. Suppose you use != as the comparison operator:
<?php
$string = 'one two';
if (strpos($string, 'one') != false) {
// Careful, this is a bug! The following line is NOT executed,
// even though it should be.
echo "The word 'one' was found in the string.\n";
}
?>
In this case strpos returns the int value 0, because it finds the word at offset 0. In the context shown, however, the != operator converts that value to a boolean. The boolean value of 0 is false — 0 is said to be “falsey”. As a result our implementation wrongly concludes that the word “one” does not occur in the given string.
About Netcup (advertisement)
The German host Netcup offers, among other things, affordable and powerful web hosting packages, KVM-based root servers and dedicated servers. With our voucher codes you can save even more (6€ off your first order, 30% off all KVM-based root servers, ...).