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

Php 正则表达式替换url编码字符串,php,regex,Php,Regex,我有一个字符串,我想用一个给定的值替换文本[[signature]],但因为它是编码的,所以文本看起来像%5B%5B签名%5D%5D 如何使用正则表达式替换它?此代码段有效,但仅当字符串未编码时有效: $replace = preg_replace('/\[\[signature\]\]/', 'replaced!', $html); 您已经对字符串进行了编码,因此只需对其进行解码,然后运行替换 $html = urldecode($html); $replace = preg_replace

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

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

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

您已经对字符串进行了编码,因此只需对其进行解码,然后运行替换

$html = urldecode($html);
$replace = preg_replace('/\[\[signature\]\]/', 'replaced!', $html);
如果需要,您可以在以后再次对其进行编码:

$html = urlencode($html);
非正则解

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

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

您已经对字符串进行了编码,因此只需对其进行解码,然后运行替换

$html = urldecode($html);
$replace = preg_replace('/\[\[signature\]\]/', 'replaced!', $html);
如果需要,您可以在以后再次对其进行编码:

$html = urlencode($html);
非正则解

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

$html = str_replace('[[signature]]', 'replaced!', $html);
那么为什么要使用正则表达式呢?在使用正则表达式之前,使用Php中可用的方法。那么为什么要使用正则表达式呢?在使用正则表达式之前,请使用Php中可用的方法。