如何阻止preg_replace()返回数字引用?

时间:2017-08-14 20:06:13

标签: php regex

我正在使用preg_replace()执行正则表达式搜索和替换。

$string = "This isn't my real string but it is a good example.";
$result = preg_replace($pattern, $replacement, $string);
echo $result; // This isn’t my real string but it is a good example.

如您所见,$string包含单引号。无论是否找到匹配项,当我输出preg_replace()的返回值时,我的单引号变为’

如何停止preg_replace()返回’等数字引用?我需要我的字符串来保留单引号字符。

更新

这是我的模式:

$pattern = '/#(\w+)/';

更新2

这是我的替换字符串:

$replacement = '<a href="https://example.com/tag/$1/">#$1</a>';

1 个答案:

答案 0 :(得分:-1)

您可以尝试使用html_entity_decode()来实现

这是一个片段

$string = "This isn't my real string but it is a good example.";
$result = preg_replace($pattern, $replacement, $string);
echo html_entity_decode($result); // This isn't my real string but it is a good example.