regex - Escaping special meaning of characters while matching patterns formed using variables: PHP:preg_match -


i performing regular expression matching find first occurrence of specific set of words in text. since don't want generate false positives when sub strings of other word, want use patterns.

for example, want find whole word "dom" not substring dom in "randomizer", say. so, using pattern "\bdom\b" consider occurrences of dom word boundary on either side. dom, ansd other such pattern strings coming array $tags. reading each tag $tag $tags, comparison be:

preg_match("/\b$tag\b/", ...) 

but trouble if $tag = ".net". "\b$tag\b" start matching strings cnet, inet, etc. interpreting . wildcard character. so, how escape special meaning of characters inside variable being used form pattern?

have @ preg_quote().

preg_quote() takes str , puts backslash in front of every character part of regular expression syntax. useful if have run-time string need match in text , string may contain special regex characters.

example #1:

$keywords = '$40 g3/400'; $keywords = preg_quote($keywords, '/'); echo $keywords; // returns \$40 g3\/400 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -