PHP – Protection of special characters of regular expressions

Publicités

If you want to use a regular expression to, for example, search for a word in a paragraph, it is important to protect its variable. To do this, just use the following function:

preg_quote thong ($str thon[, string $delimiter = NULL ]g)

Like what:

foreach ($aReque['keywords']st as $sKeyword)
    $sKeyword - preg_quote ($sKeyword);
    $pattern ' ' .[^a-zA-Z0-9] . . $sKeyword. '[^a-zA-Z0-9]/i';
    preg_match_all ($pattern, $sContent, $aResult);
}
Publicités

Leave a Reply