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 :: laravel default rate limit 
Php :: how to create object in php 
Php :: php code generator 
Php :: xampp php ini 
Php :: laravel + join 2 eloquent queries 
Php :: iterator 
Php :: php increment variable by 1 
Php :: route parameter type laravel 
Php :: route group laravel 8 
Php :: string to lowercase accentuation hyphenated 
Php :: preared request pdo 
Php :: why php is not using datatype 
Php :: PHP metaphone — Calculate the metaphone key of a string 
Php :: google sheets to php equation 
Php :: $age = 20; print ($age = 18) ? "Adult" : "Not Adult"; 
Php :: WordPress Image/Files uploads 
Php :: join with 2 table where id match in table 1 comma separated 
Php :: echo two variables same line php 
Php :: redirect www to non-www wordpress multisite 
Php :: pass variable in laravel ancher tag laravel 8 
Php :: how to select specific id in laravel using isset 
Php :: wpdb insert or if exists update 
Php :: codeigniter AES _ENCRYPT or AES_DECRYPT in where 
Php :: Nginx + Laravel - Moving blog from subdomain to /blog 
Php :: Unregistering a variable with $_SESSION. 
Php :: how-to-add-pagination-in-search-results wordpress 
Php :: laravel livewire public property 
Php :: without login cant purchase woocommerce 
Php :: php upload image to another subdomain 
Php :: wp wc php sort products archive cheapest price 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =