2012-07-02 57 views
-1

在管理使用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; 
?> 
+0

这是我第一次看到在模板之外使用的语法。 – sachleen

+0

@mario,preg_match_callback()不存在。 –

+0

是的,我很笨。它是'preg_replace_callback'。 - 使用['preg_replace_callback()'](http://php.net/preg_replace_callback)。那么你不需要循环。只需在回调中打包替换代码即可。 (你的实际问题可能是缺乏正则表达式转义/分隔符/和除此之外,禁用error_reporting) – mario

回答

2

也许,你preg_replace -ing不使用模式:$matches[0][$c]持有字符串,而不是带分隔符的图案。再一次,我可能是错的。看看你的匹配和你正在替换什么可能会有帮助

nip,我不能相信我忽略了循环内的数组声明......当然,这是你的第一件事应该修复!

5

您需要定义$pattern$replacement之外循环的,否则他们将得到重新初始化为空数组在每次迭代:

$pattern = array(); $replacement = array(); 
for($c=0;$c<$num_matches;$c++): 
+0

做到了,但它不会显示任何内容。我认为它会显示$主体不变,但会发生什么,它仍然不显示任何内容。 –

0

谢谢你的答案的家伙,但我停止使用了preg_replace,我改用str_replace。它工作正常。这是我的最终代码。

<?php 
     $tweets = getTweets($profile->twitter_id); 

     for($i=0;$i<4;$i++): 
      // first, explode the description to eliminate the username in the description 
      $tweet_des = $tweets[$i]->description; 
      $tweet_no_username_des = explode(':',$tweet_des,2); // added a limit parameter, ensuring only the username will be excluded 
      $new_tweet_description = $tweet_no_username_des[1].'<br />'; 

      // the date the tweet is published 
      echo $date_pub = $tweets[$i]->pubDate; 

      // using preg_match_all to find matches, texts having '@' as the first letter 
      $num_matches = preg_match_all('/@\w+/', $tweets[$i]->description, $matches); 
      if($num_matches): // if match(es) are found 
       $search_arr = array(); // declared an array to contain the search parameter for str_replace 
       $replace_arr = array(); // declared an array to contain the replace parameter for str_replace 
       for($c=0;$c<$num_matches;$c++): 
        $search_arr[$c] = $matches[0][$c]; 
        $name_links[$c] = explode('@',$search_arr[$c]); 
        $not_link_name = $name_links[$c][1]; 
        $replace_arr[$c] = '<a href="http://twitter.com/#!/'.$not_link_name.'" target="_blank">'.$matches[0][$c].'</a>'; 
       endfor; 
       echo auto_link(str_replace($search_arr, $replace_arr, $new_tweet_description)).'<br />'; 
      else: 
       echo auto_link($new_tweet_description).'<br />'; 
      endif; 
     endfor; 
    ?> 

最初我的问题是找到以'@'开头的文本,并将其链接到Twitter中的相应帐户。请随时批评我的代码,但我认为它仍然是越野车。 :)