gpt4 book ai didi

php - 正则表达式查找包含自定义符号的单词或句子

转载 作者:行者123 更新时间:2023-12-04 15:14:02 24 4
gpt4 key购买 nike

我需要替换一些单词或句子并将其转换为下划线。我正在使用 PHP 并找到一个引用:PHP Regex, extract all custom tags from text

不知何故,该案例仅涵盖单个单词而不是句子。如何使正则表达式也可以捕获 ## 所包含的所有内容标签?

假设我的输入将是:

"ll the Lorem Ipsum ##generators## on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ##200 Latin words##, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ##free from repetition##, injected humour, or non-characteristic words etc."



然后输出将是:

"ll the Lorem Ipsum ____1____ on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over ____2____, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always ____3____, injected humour, or non-characteristic words etc."



谁能帮助我如何获得正则表达式模式?

最佳答案

使用@shA.t 的正则表达式的另一个想法是使用 preg_replace_callback 。使用增加变量的函数。所以它可以在没有循环的情况下完成,这可能会提高效率。

$str = preg_replace_callback('/##[^#]+##/', function($m) use (&$i) {
return "____". ++$i ."____";
}, $str);

See php demo at eval.in

关于php - 正则表达式查找包含自定义符号的单词或句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45688677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com