[英]preg_replace return in variable
在这里,我有我的正则表达式,它找到4chan样式引用的所有实例(例如> 10,> 59,> 654564),并将其作为模式输出返回。 我的问题是,是否可以插入我的图案输出 ...
\\ 1
...转换为PHP函数。
虽然这很好用:
$a = preg_replace('`(>\d+)`i', '\1', $b);
我要找的东西不是:
$a = preg_replace('`(>\d+)`i', '".getpost('\1')."', $b);
示例[php> = 5.3.0] ( 使用Closure ):
$callback = function($match) {
return "{" . $match[1] . "}"; # do smth with match
};
$string = 'test1 >1 test2 >12 test3 >123 test4';
echo preg_replace_callback('~(>\d+)~i', $callback, $string);
将输出:
test1 {>1} test2 {>12} test3 {>123} test4
示例[php <5.3.0] :
function replaceCallback($match) {
return "{" . $match[1] . "}"; # do smth with match
};
$string = 'test1 >1 test2 >12 test3 >123 test4';
echo preg_replace_callback('~(>\d+)~i', 'replaceCallback', $string);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.