我想用一些函数替换文本中的项目。例如,我想从WYSIWYG中放置滑块我写的是像这样的{SLIDER_8879},其中SLIDER是函数类型,8879是参数(例如文件夹)。
例如:
<?php
function xxx($fname, $value){
echo"<span style='color:orange;'>$fname|$value this is CAROUSEL</span> ";
}
function callback ($match) {
if ($match[2] === "SLIDER"){
return "<span style='color:red'>This is a slider</span>";
}else if($match[2] === "POLL"){
return rand(8888888888, 8888888888888888888888);
}else if($match[2] === "CAROUSEL"){
xxx($match[2], $match[4]);
}else{
echo"<span style='color:blue'>ESIM INCH!</span>";
}
}
$body = "{{SLIDER_fff}} sdfasdfas dfasd {{POLL_8879}} dsgasdgasdgds {{CAROUSEL_888}} sdffasd f asd f a {{SLIDER_8879-folder}} dfgdf g dsfsfgfg d fdfgd {{BIBIN_folder}} sdfsdf";
$pattern = "/({{2})([A-Z]+)(_{1})([a-zA-Z0-9_\-]+)(}{2})/";
$body = preg_replace_callback($pattern, "callback", $body);
echo $body;
?>
我得到了这个订单:
CAROUSEL | 888这是CAROUSEL ESIM INCH!这是一个滑块sdfasdfas dfasd 123055895 dsgasdgasdgds sdffasd f asd f a这是一个滑块dfgdf g dsfsfgfg d fdfgd sdfsdf
想拥有正确的替换订单:
这是一个滑块sdfasdfas dfasd 123055895 dsgasdgasdgds CAROUSEL | 888这是CAROUSEL sdffasd f asd f a这是一个滑块dfgdf g dsfsfgfg d fdfgd ESIM INCH! sdfsdf
答案 0 :(得分:0)
回调应该返回替换字符串。
也许这个更新的代码正是您所寻找的:
<?php
function xxx($fname, $value){
return "<span style='color:orange;'>$fname|$value this is CAROUSEL</span> ";
}
function callback ($match) {
if ($match[2] === "SLIDER"){
return "<span style='color:red'>This is a slider</span>";
}else if($match[2] === "POLL"){
return rand(8888888888, 8888888888888888888888);
}else if($match[2] === "CAROUSEL"){
return xxx($match[2], $match[4]);
}else{
return "<span style='color:blue'>ESIM INCH!</span>";
}
}
$body = "{{SLIDER_fff}} sdfasdfas dfasd {{POLL_8879}} dsgasdgasdgds {{CAROUSEL_888}} sdffasd f asd f a {{SLIDER_8879-folder}} dfgdf g dsfsfgfg d fdfgd {{BIBIN_folder}} sdfsdf";
$pattern = "/({{2})([A-Z]+)(_{1})([a-zA-Z0-9_\-]+)(}{2})/";
$body = preg_replace_callback($pattern, "callback", $body);
echo $body;
?>