I have a text file of HTML that includes the following marker set for replacement:
$$ausposttrackingid$$
I’m trying to replace that text using the following regex:
$trackingId="M6023409823490234";
$text = preg_replace("/\$\$ausposttrackingid\$\$/",$trackingId,$text);
But…it doesn’t replace the text at all, leaving the $$ausposttrackingid$$ alone.
I thought I’d escaped the $ signs properly, but no give.
Can anyone tell me how I should specify the regex in preg_replace to correctly pick up the $$ausposttrackingid$$? Or, alternately, should I use another tag marker instead of $ given its meaning in regexes?
Thanks guys.
Pete
Don’t use double quotes, because PHP will look for variable names inside your string and replace them for their value. So
$ausposttrackingidin the string gets replaced with the value of the variable$ausposttrackingid. If that isn’t your intention, use single quotes:In fact, get used to use single quotes, unless needed.
A working example:
Addendum
You will want to use double quotes for some scape characters, for instance
"\n"will give you a new line but'\n'will not, instead it will just print\n.