Martin Henriksen Martin Henriksen - 28 days ago 4x
PHP Question

Regex replace url encoded string

I have a string in which I want to replace the text

with a given value, but because it is encoded, the text looks like

How do I replace this using a regular expression? This snippet works, but only if the string is not encoded:

$replace = preg_replace('/\[\[signature\]\]/', 'replaced!', $html);


You have encoded the string, so just decode it then run your replace.

$html = urldecode($html);
$replace = preg_replace('/\[\[signature\]\]/', 'replaced!', $html);

You can always encode it again afterwards if you need:

$html = urlencode($html);

Non-Regex Solution

If your find/replace is really that simple then you don't even need to use regex. Just do a standard string replace:

$html = str_replace('[[signature]]', 'replaced!', $html);