gpt4 book ai didi

php - 如何在 preg_replace 中用 c(xxx) 替换 c(xxx)?

转载 作者:可可西里 更新时间:2023-10-31 22:55:47 25 4
gpt4 key购买 nike

如何在preg_replace中用c(xxx)替换c(xxx)

在下面的代码中,我想将 str c(xxx) 替换为 functioun c(xxx)
我没有得到我想要的正确结果。
我的代码有什么问题?以及如何解决?

$c['GOOD']='very good';
$c['BOY']='jimmy';
function c($x){
global $c;
if(isset($c[$x])){
return $c[$x];
}
}

$str="hello c(GOOD) world c(BOY) ";
$str=preg_replace("@c\(([A-Z_\d]+)\)@",c('$1'),$str);
echo $str; // --> hello world

// how to get hello very good world jimmy

最佳答案

php.net: preg_replace_callback()

如果您查看示例 #2,这就是您要查找的内容:)

function c($matches){
print_r($matches)
}
$str = preg_replace_callback("@c\(([A-Z_\d]+)\)@", 'c', $str);

小旁注:我不知道你是否想调用你的函数'c',但我建议使用明确的函数名来解释它们的作用

关于php - 如何在 preg_replace 中用 c(xxx) 替换 c(xxx)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17852896/

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