Search
 
SCRIPT & CODE EXAMPLE
 

PHP

How do I check if a string contains a specific word?

$a = 'Hello world?';

if (strpos($a, 'Hello') !== false) { //PAY ATTENTION TO !==, not !=
    echo 'true';
}
if (stripos($a, 'HELLO') !== false) { //Case insensitive
    echo 'true';
}
Comment

check if a string contains a word

//By far the most accurate: VIA https://stackoverflow.com/a/25633879/7596555
function containsWord($str, $word)
{
    return !!preg_match('#' . preg_quote($word, '#') . '#i', $str);
}

Comment

How do I check if a string contains a specific word?



While most of these answers will tell you if a substring appears in your string, that's usually not what you want if you're looking for a particular word, and not a substring.

What's the difference? Substrings can appear within other words:

The "are" at the beginning of "area"
The "are" at the end of "hare"
The "are" in the middle of "fares"
One way to mitigate this would be to use a regular expression coupled with word boundaries ():

function containsWord($str, $word)
{
    return !!preg_match('#' . preg_quote($word, '#') . '#i', $str);
}
This method doesn't have the same false positives noted above, but it does have some edge cases of its own. Word boundaries match on non-word characters (W), which are going to be anything that isn't a-z, A-Z, 0-9, or _. That means digits and underscores are going to be counted as word characters and scenarios like this will fail:

The "are" in "What _are_ you thinking?"
The "are" in "lol u dunno wut those are4?"
If you want anything more accurate than this, you'll have to start doing English language syntax parsing, and that's a pretty big can of worms (and assumes proper use of syntax, anyway, which isn't always a given).
Comment

How do I check if a string contains a specific word?

$haystack = 'How are you?';
$needle   = 'are';

if (strpos($haystack, $needle) !== false) {
    echo 'true';
}
Comment

PREVIOUS NEXT
Code Example
Php :: check value falls between in two range in php 
Php :: how to use md5 in php 
Php :: get current url in controller in laravel 
Php :: php run localhost 
Php :: return view controller laravel 
Php :: php convert string to url 
Php :: wordpress get template directory 
Php :: laravel artisan cache clear 
Php :: codeigniter redirect 
Php :: laravel blade get array count in Blade 
Php :: how to delete all products woocommerce in phpmyadmin 
Php :: sum of columns laravel eloquent 
Php :: In PackageManifest.php line 122: 
Php :: lat long in laravel validation 
Php :: how to use where relationship laravel 
Php :: 15000 tl to usd 
Php :: php isset multiple 
Php :: add hour minute in datetime in php 
Php :: symfony redirect to previous page 
Php :: time php 
Php :: Convert String to Date and Date-Time in PHP 
Php :: save an image use php 
Php :: 18 year back date in php 
Php :: laravel unique multiple columns 
Php :: laravel asset 
Php :: how to echo only certain character number in php 
Php :: how get the Photo size upload in laravel 
Php :: get public_html directory php 
Php :: cambiar entre versiones de php linux 
Php :: acf get field 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =