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); Quote 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. Quote 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. 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.