正则表达式替换url编码的字符串

时间:2016-09-23 09:44:12

标签: php regex

我有一个字符串,其中我想用给定的值替换文本[[signature]],但因为它是编码的,所以文本看起来像%5B%5Bsignature%5D%5D

如何使用正则表达式替换它?此代码段有效,但仅当字符串未编码时才会起作用:

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

1 个答案:

答案 0 :(得分:4)

您已对字符串进行了编码,因此只需对其进行解码即可运行替换。

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

如果需要,您可以随后再次对其进行编码:

$html = urlencode($html);

非正则表达式解决方案

如果您的查找/替换非常简单,那么您甚至不需要使用正则表达式。只需做一个标准字符串替换:

$html = str_replace('[[signature]]', 'replaced!', $html);
相关问题