Jump to content

how to search for patterns with special characters?


mctrivia

Recommended Posts

The following code works as long as the $tag variable does not contain a $ symbole(and presumably other special symbols as well)  how can I easily replace all special symbols with there regexp equivalents?  Is there a useful function that I can't find like mysql_real_escape_string works for mySQL?

 

$this->code = preg_replace('/<TEMPLATE_' . $tag . '>/',$value . '',$this->code);

this may not be the best way but it works:

$from=array('/\./','/\+/','/\*/','/\?/','/\^/','/\$/','/\[/','/\]/','/\(/','/\)/','/\|/','/\{/','/\}/','/\//',"/\'/",'/\#/');
$to=array('\\\.','\\\+','\\\*','\\\?','\\\^','\\\$','\\\[','\\\]','\\\(','\\\)','\\\|','\\\{','\\\}','\\\/',"\\\'",'\\\#');
$tag = preg_replace($from,$to,$tag);
$this->code = preg_replace('/<TEMPLATE_' . $tag . '>/',$value . '',$this->code);

 

goes through and adds a \ in front of all(I think) the special characters.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.