PHP preg_替换替换中的任何文本

PHP preg_替换替换中的任何文本,php,regex,preg-replace,replace,Php,Regex,Preg Replace,Replace,如何将文本替换为替换中的任何文本和符号?因为当我使用反斜杠时,它被严重替换了 $text = 'hello replacement world'; $text = preg_replace('#replacement#ui', '28\01\12', $text); 结果 hello 28 world 在使用模式之前进行替换的解决方案 $pattern = '28\01\12'; $pattern = str_replace('\\', '\\\\\\\\', $pattern); 这只

如何将文本替换为替换中的任何文本和符号?因为当我使用反斜杠时,它被严重替换了

$text = 'hello replacement world';

$text = preg_replace('#replacement#ui', '28\01\12', $text);
结果

hello 28 world
在使用模式之前进行替换的解决方案

$pattern = '28\01\12';
$pattern = str_replace('\\', '\\\\\\\\', $pattern);
这只是一个例子,在实际工作中需要使用更复杂的模式,替换将包括任何文本

$text = preg_replace('#replacement#ui', '28\\01\\12', $text);
请参见此处关于反斜杠的注释:

如果只搜索一个简单的字符串来替换,则应该考虑使用<代码> StryRePipe()/<代码>。它的cpu成本更低。请看这里:

更新:有关转义序列的更多信息:

请参见此处关于反斜杠的注释:

如果只搜索一个简单的字符串来替换,则应该考虑使用<代码> StryRePipe()/<代码>。它的cpu成本更低。请看这里:

更新:有关转义序列的更多信息:

这是因为必须转义字符串中的反斜杠


发生这种情况是因为您必须转义字符串中的反斜杠。

为什么要使用preg\u replace?在这种情况下,简单的str_替换会更快。例如,模式更复杂。为什么要使用preg_替换?对于这种情况,一个简单的str#u替换会快得多。例如,模式更复杂。好的,使用$text=preg#u替换(“#replacement#ui”,“28\\\\01\\\\12”,“$text”);我需要使用更复杂的preg_替换原因模式。请告诉我,为了避免出现问题,我还需要避开哪些符号?好的,与$text=preg#u replace(“#replacement#ui”,“28\\\\01\\\\12”,“$text”)一起使用;我需要使用更复杂的preg_替换原因模式。请告诉我,为了避免出现问题,我还需要避开哪些符号?好的,与$text=preg#u replace(“#replacement#ui”,“28\\\\01\\\\12”,“$text”)一起使用;我需要使用更复杂的preg_替换原因模式。请告诉我,为了避免出现问题,我还需要避开哪些符号?好的,与$text=preg#u replace(“#replacement#ui”,“28\\\\01\\\\12”,“$text”)一起使用;我需要使用更复杂的preg_替换原因模式。请告诉我,为了避免问题,我还需要逃避什么符号?
$text = 'hello replacement world';

$text = preg_replace('#replacement#ui', '28\\01\\12', $text);