0
我想用preg_replace_callback
替换句子中的单词。使用preg_replace_callback和create_function
"%1% and %2% went up the %3%"
应该成为
"Jack and Jill went up the hill"
我已经下面给出我的代码。
<?php
$values = array("Jack", "Jill", "hill");
$line = "%1% and %2% went up the %3%";
$line = preg_replace_callback(
'/%(.*?)%/',
create_function(
// single quotes are essential here,
// or alternative escape all $ as \$
'$matches',
'return $values[$matches[1]-1];'
),
$line
);
echo $line;
?>
我所得到的是
" and went up the "
如果我给return $matches[1]-1;
,我越来越
"0 and 1 went up the 2"
它是一个范围的问题吗?如何使这个工作? 任何帮助,将不胜感激。