-1

我有以下代码

return preg_replace_callback(
    "#\{gallery: '(.+?)'(?: dir: ([0-1]))?\}#i",
    create_function('$i', 'echo $i[1];' ),
    $string);

我的问题是,如果我的字符串看起来像这样:

top
{gallery: 'images/'}
center
{gallery: 'images/characters'}
bottom

当它被渲染时,它看起来像这样:

images/
images/characters
top center bottom

为什么要更改顺序并将替换的代码放在顶部,而将其他所有内容放在底部,甚至是中间的东西?

4

1 回答 1

2

您应该return在替换回调中使用语句:

$string = "top {gallery: 'images/'} center {gallery: 'images/characters'} bottom";
$string = preg_replace_callback(
  "#\{gallery: '(.+?)'(?: dir: ([0-1]))?\}#i", 
  create_function('$i', 'return $i[1];'), 
  $string
);
echo $string . PHP_EOL;

// Outputs: top images/ center images/characters bottom
于 2012-07-17T05:23:18.483 回答