I have been trying to match my one variable with the other. All I want that variable let's say $word
contains no matter what in it matches with the the other variable lets say $search_in
and echo results accordingly.
I have been using if (preg_match("/$word/i", $search_in)) echo "matches";
but I am getting a lot of warnings like the following as I applied regexp on the values being returned from DB.
Warning: preg_match() [function.preg-match]: Unknown modifier '('
Warning: preg_match() [function.preg-match]: Unknown modifier 'T'
$word
most likely contains a slash in it.
You should use stripos
for this, since you don't need the power of regular expressions at all:
if (stripos($search_in, $word) !== false) echo 'matches';
For the record, the preg_match
way of doing it would be:
if (preg_match('/'.preg_quote($word, '/').'/i', $search_in)) echo 'matches';
Always prefer the string functions to regular expressions when the former can do the job.
If you just need to check whether $word
exists in $search_in
, you can use:
if(stripos($search_in, $word) !== FALSE) echo "matches";
As the PHP manual says:
Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.