gpt4 book ai didi

php - preg_replace 之间的某些东西而不删除某些东西

转载 作者:行者123 更新时间:2023-12-03 19:37:53 24 4
gpt4 key购买 nike

我要做的是:

function replace_between_something($betweenWhat,$replace_thing,$target){
return preg_replace("/".$betweenWhat."(.*?)".$betweenWhat."/",$replace_thing,$target);
}
$string="Hi, *my* name is 'Soul' and she is 'Maggie'. ";

$replace=replace_between_something("\*","your",$string);
$replace=replace_between_something("'","noname",$replace);

echo $replace;

预期的输出是:

Hi, *your* name is 'noname' and she is 'noname'.

但是,实际输出(与预期不同):

Hi, your name is noname and she is noname.

如何保留符号??

有人可以帮助我吗???

抱歉,我只是编辑我的问题以展示我想做的实际事情。

最佳答案

您可以在替换字符串中再次包含引号:

$string="Hi, my name is 'Soul' and she is 'Maggie'. ";
$replace=preg_replace("/'(.*?)'/","'noname'",$string);
echo $replace;

关于php - preg_replace 之间的某些东西而不删除某些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33841410/

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