gpt4 book ai didi

preg-replace - php 中的 Preg_replace - 多个替换

转载 作者:行者123 更新时间:2023-12-05 07:55:57 26 4
gpt4 key购买 nike

我有 $string,其中包含:

this example

我有这 3 个表达式:

$pattern = array('/aa*/','/ii*/');
$replacement = array('<i>$0</i>','<b>$0</b>');
preg_replace($pattern, $replacement, $string);

其中,preg_replace 返回:

th<b>i</b>s ex<<b>i</b>>a</<b>i</b>>mple

我需要这样的输出:

th<b>i</b>s ex<i>a</i>mple

这意味着,我只想替换原始字符串中的字符。可能吗?

最佳答案

这在我的测试中起到了作用

$pattern = array('/([a-z|^|\s])(aa*)/', '/([a-z|^|\s])(ii*)/');
$replacement = array('$1<i>$2</i>','$1<b>$2</b>');

关于preg-replace - php 中的 Preg_replace - 多个替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29089670/

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