简体   繁体   English

PHP preg_replace certrain字符串

[英]PHP preg_replace certrain strings

I have several of these tags in a text document (stored in $content) with a lot of other content as well: 我在文本文档(存储在$ content中)中有几个这样的标签,还有很多其他内容:

[[tag:author|id:6329]]

And I've been trying to replace all of these tags with a given id (in this case 6329) with another string. 我一直在尝试用另一个字符串替换具有给定id(在本例中为6329)的所有这些标签。

$id = 6329;
$replacement = $this->format($id);
$pattern = "/(\[\[tag:author|id:".$id."\]\])/";
preg_replace($pattern, $replacement, $content);

Although this does not seem to work, and I've been tearing my hair out for the last couple of hours. 尽管这似乎行不通,但在过去的几个小时中,我一直在梳理头发。 Can anyone see the error? 谁能看到错误? Many thanks. 非常感谢。

You need to escape | 您需要逃脱| too, and not necessary to wrap it with () . 同样,也不必用()包裹它。

Try: 尝试:

$pattern = "/\[\[tag:author\|id:".$id."\]\]/";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM