[英]how to replace content from ({{}}) Special character in php
I Have a Paragraph that contains some content and also values inside of {{0001ABC}} I want to replace {{0001ABC}} with some content for example code Found. 我有一个段落,其中包含一些内容以及{{0001ABC}}内部的值,我想用一些内容(例如找到的代码)替换{{0001ABC}}。
preg_replace('/{{"([^\\"]+)"}}/', "Code found", "Hello {{0001ABC}}");
The result should look like this 结果应如下所示
Hello Code found
Your regex pattern is incorrect. 您的正则表达式模式不正确。 Here is a version which should work:
这是应该工作的版本:
\{\{[^{}]+\}\}
This will match two opening curly braces, followed by one or more characters which are not curly braces, followed by two closing curly braces. 这将匹配两个大括号,然后是一个或多个不是大括号的字符,然后是两个大括号。 Keep in mind that
{
and }
are regex metacharacters , have a special meaning, and therefore need escaping if we want them to mean literal characters. 请记住,
{
和}
是正则表达式元字符 ,具有特殊含义,因此如果我们希望它们表示文字字符,则需要转义。
Your updated script: 您更新的脚本:
$input = "Hello {{0001ABC}}";
$output = preg_replace("/\{\{[^{}]+\}\}/", "Code found", "Hello {{0001ABC}}");
echo $output;
This prints: 打印:
Hello Code found
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.