match in php code example
Example 1: preg_match in php
<?php
$str = "Check For Testing.";
if (preg_match("/\bCheck\b/i", $str, $match))
echo "Matched!";
else
echo "not matched";
?>
Example 2: string match in php
if (str_contains('Foo Bar Baz', 'Foo')) {
echo 'Found';
}
Example 3: php regex
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
Example 4: string match in php
You could use regular expressions as its better for word matching compared to
strpos, as mentioned by other users. A strpos check for are will also return
true for strings such as: fare, care, stare, etc. These unintended matches can
simply be avoided in regular expression by using word boundaries.
A simple match for are could look something like this:
$a = 'How are you?';
if (preg_match('/\bare\b/', $a)) {
echo 'true';
}