简体   繁体   中英

Regular expression (search and replace) with text from MySQL

In PHP I want to remove '\ABC{' and '}' from all places in a text string, eg

This is a short \ABC{text} with some \ABC{codes} that I want to have \ABC{removed}.

is to become

This is a short text with some codes that I want to have removed.

I've tried the following in PHP;

    $string = "This is a short \ABC{text} with some \ABC{codes} that I want to have \ABC{removed}";
    $pattern = '/\\\\ABC{(\w+)}/';
    $replacement = '\1';
    $str = preg_replace($pattern, $replacement, $string);
    echo $str;

and it works well. However, when the $string text is fetched from a MySQL db instead, no replacement is done. What could be wrong? TIA.

You can use T-Regx :

echo Pattern::inject('@(\w+)@', ['\ABC{', '}'])->replace($string)->all()->by()->group(1)->orThrow();

You can see demo here: https://repl.it/repls/HorizontalGenerousApplicationpackage

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