在管理使用preg_match_all之后,我想要用matchedText替换匹配的文本,但是它似乎不起作用.看看我的代码,我做错了什么?
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
您需要定义$pattern和$replacement循环外,否则他们将得到重新初始化,在每个迭代一个空数组:
$pattern = array(); $replacement = array();
for($c=0;$c<$num_matches;$c++):
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |