1

I have a string in which I want to replace the text [[signature]] with a given value, but because it is encoded, the text looks like %5B%5Bsignature%5D%5D.

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);
2

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.