How Do I Check If A String Contains A Specific

[Solved] How Do I Check If A String Contains A Specific | Swift - Code Explorer | yomemimo.com
Question : How do I check if a string contains a specific word?

Answered by : matteo-puppis

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

Source : https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word | Last Update : Mon, 25 May 20

Question : check if a string contains a word

Answered by : outrageous-ocelot-tpydbi6iw95m

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

Source : | Last Update : Thu, 02 Dec 21

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

Answered by : shafeeque-ahmad

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 (\b):
function containsWord($str, $word)
{ return !!preg_match('#\\b' . preg_quote($word, '#') . '\\b#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).

Source : https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word | Last Update : Fri, 04 Mar 22

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

Answered by : energetic-emu-5snqv9jpdaca

$haystack = 'How are you?';
$needle = 'are';
if (strpos($haystack, $needle) !== false) { echo 'true';
}

Source : | Last Update : Mon, 19 Sep 22

Answers related to how do i check if a string contains a specific word

Code Explorer Popular Question For Swift