简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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