mctrivia Posted January 16, 2011 Share Posted January 16, 2011 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); Link to comment https://forums.phpfreaks.com/topic/224625-how-to-search-for-patterns-with-special-characters/ Share on other sites More sharing options...
mctrivia Posted January 16, 2011 Author Share Posted January 16, 2011 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. Link to comment https://forums.phpfreaks.com/topic/224625-how-to-search-for-patterns-with-special-characters/#findComment-1160367 Share on other sites More sharing options...
requinix Posted January 16, 2011 Share Posted January 16, 2011 The best way would be with preg_quote. Link to comment https://forums.phpfreaks.com/topic/224625-how-to-search-for-patterns-with-special-characters/#findComment-1160368 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.