<code>$a = " #0 and #1 or (#0)";
$items = array(
"a>1",
"b>0"
);
$str = preg_replace("/#(\w+)/","\$items[$1]",$a);
echo $str;</code>输出结果: $items[0] and $items[1] or ($items[0])
怎么才能让输出结果是:a>1 and b>0 or (a>1)?
请各位大神帮忙看一下,谢谢!
<code>$a = " #0 and #1 or (#0)";
$items = array(
"a>1",
"b>0"
);
$str = preg_replace("/#(\w+)/","\$items[$1]",$a);
echo $str;</code>输出结果: $items[0] and $items[1] or ($items[0])
怎么才能让输出结果是:a>1 and b>0 or (a>1)?
请各位大神帮忙看一下,谢谢!
正则
立即学习“PHP免费学习笔记(深入)”;
<code>$items = [
'a > 1',
'b > 0',
'abc' => 'c > 1'
];
$subject = '#0 and #1 or (#0) and #abc #nooo';
echo preg_replace_callback('/#([a-z0-9_]+)/i', function($v) use ($items){
return isset($items[$v[1]]) ? $items[$v[1]] : $v[0];
}, $subject);</code><code>a > 1 and b > 0 or (a > 1) and c > 1 #nooo</code>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号