繁体   English   中英

使用来自 MySQL 的文本的正则表达式(搜索和替换)

[英]Regular expression (search and replace) with text from MySQL

在 PHP 中,我想从文本字符串中的所有位置删除 '\ABC{' 和 '}',例如

这是一个简短的 \ABC{text},其中包含一些我想要 \ABC{removed} 的 \ABC{codes}。

是成为

这是一段简短的文本,其中包含一些我想删除的代码。

我在 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;

它运作良好。 但是,当从 MySQL 数据库中获取 $string 文本时,不会进行任何替换。 有什么问题吗? TIA。

您可以使用T-Regx

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

你可以在这里看到演示: https://repl.it/repls/HorizontalGenerousApplicationpackage

暂无
暂无

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

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