Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP error when input contains "/"

Tags:

php

preg-match

When the input string contains a "/" symbol, I'm getting a PHP error:

Warning: preg_match() [function.preg-match]: Unknown modifier

How can I resolve it?

$token = '/<'.$tag.'[^>]*>(.*\b'.$keyword.'\b.*)<\/'.$tag.'>/siU';
if(preg_match($token, &$content, $matches))
{
    $match = 1;
}
return $match;
like image 876
RegEdit Avatar asked Mar 11 '26 23:03

RegEdit


2 Answers

You should use preg_quote() to escape tag and keyword, like this:

$token = '/<' . preg_quote($tag, '/') . '[^>]*>(.*\b' . preg_quote($keyword, '/')
         . '\b.*)<\/' . preg_quote($tag, '/') . '>/siU';
like image 88
Vyktor Avatar answered Mar 14 '26 12:03

Vyktor


preg_quote function should help you with this.

like image 32
Slawek Avatar answered Mar 14 '26 11:03

Slawek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!