I have a variable which will contain a relative URL (i.e. ../test.html) and I want to use that url variable and replace it with an absolute variable. Anyway I want to know if there is a more elegant way escape a string when it contains characters that need to be escaped in a regular expression. I got the following to work:
str_replace(array(".","/"), array("\\.","\\/"), $urls[0][$key])
But I think there is a better way to do this with preg_replace but I can’t seem to get it to work. I’ve tried the following using a back reference:
preg_replace('/[\.\/]/i', '\\\1', $urls[0][$key])
That does not seem to work, probably because of the way that PHP is escapes some characters in the preg_replace:
When using the e modifier, this function escapes some characters (namely ‘, “, \ and NULL) in the strings that replace the backreferences.
Anyone know how I can get the preg_replace to work?
Thanks for your help!
preg_quote. Also, not everything is a variable. It’s not an absolute variable, you’re just replacing the variable with a string representing an absolute path..