gpt4 book ai didi

PHP:多次替换两个字符之间的内容

转载 作者:行者123 更新时间:2023-12-05 03:06:11 24 4
gpt4 key购买 nike

我试图在每次出现时替换某些“特殊”字符之间的内容。例如,假设我有那个字符串:

<div><small>static content</small><small>[special content type 3]</small> 
<small>static content</small><small>[special content type 4]</small></div>

我想用这个“标识符”(比方说,一些“小部件”)表示的内容替换方括号之间的每个“特殊内容”。

我试过 Stackoverflow 中的这段代码:

$search = "/[^<tag>](.*)[^<\/tag>]/";
$replace = "your new inner text";
$string = "<tag>i dont know what is here</tag>";
echo preg_replace($search,$replace,$string);

这有效,但仅适用于第一次出现。我需要在整个字符串中重复此操作。

这个我也试过:

echo preg_replace('/<div class="username">.+?</div>/im', '<div 
class="username">Special Username<\/div>', $string) ;

它给我一个“警告:preg_replace():第 8 行标准输入代码中的未知修饰符‘d’”错误。

有什么想法吗?

最佳答案

您在 stackoverflow 中的代码需要稍作改动:

$search = "/(<tag>)(.*?)(<\/tag>)/";
$replace = '$1your new inner text$3';
$string = "<tag>i dont know what is here</tag> some text <tag>here's another one</tag>";
echo preg_replace($search,$replace,$string);

关于PHP:多次替换两个字符之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49806486/

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