在管理使用preg_match_all之后,我想用matchedText替换匹配的文本,但它看起来不起作用。看看我的代码,我做错了什么?preg_replace有问题吗?
<?php
$tweets = getTweets($profile->twitter_id);
for($i=0;$i<4;$i++):
// finds matches
$num_matches = preg_match_all('/@\w+/', $tweets[$i]->description, $matches);
if($num_matches):
echo $num_matches.'<br />';
for($c=0;$c<$num_matches;$c++):
$subject = $tweets[$i]->description;
$pattern = array();
$pattern[$c] = $matches[0][$c];
$replacement = array();
$replacement[$c] = '<a href="https://twitter.com/#!/">'.$matches[0][$c].'</a>';
endfor;
echo preg_replace($pattern, $replacement, $subject).'<br /><br />';
else:
echo auto_link($tweets[$i]->description).'<br /><br />';
endif;
endfor;
?>
这是我第一次看到在模板之外使用的语法。 – sachleen
@mario,preg_match_callback()不存在。 –
是的,我很笨。它是'preg_replace_callback'。 - 使用['preg_replace_callback()'](http://php.net/preg_replace_callback)。那么你不需要循环。只需在回调中打包替换代码即可。 (你的实际问题可能是缺乏正则表达式转义/分隔符/和除此之外,禁用error_reporting) – mario