开发者

need help troubleshooting a function that searches for specific url links in an array of text

hey guys Im building a script by which Im attempting to find specific links in twitter text results

This script basically checks whether the text contains a url, then determines if that url is one of 6 specific urls, and if it matches outputs the original text into a new array Ive labeled as $imgtweets

the problem however is that despite the fact that I have about 4 text string in the array only one of them is matching and being returned in the $imgtweets array, Im having a hard time determining where Ive made the mistake, any help would go a long way!

this is my code, Ive had to make the array slightly smaller however because im not allowed to post more that 2 hyperlinks at this point:

<?php

$tweets = array(
         "Photo: therulesofagentleman: http://tumblr.com/xc52sgx6u7",
         "http://mypict.me/iysEX So this is Karly. Karly say hello to the world. We've been at this a while when your fans (cont)",
         "this is some test text that doesnt contain any links for testing purposes");

         $imgtweets = array();

         foreach($tweets as $tweet) {
         preg_match_all("#开发者_如何学编程(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", $tweet, $matches, PREG_PATTERN_ORDER);

         $tweetlinks = $matches[0];
         if (!empty($tweetlinks)){

         foreach($tweetlinks as $key => $link) {

         if (substr($link,0,14)=="http://lockerz" || substr($link,0,12)=="http://yfrog"
         || substr($link,0,14)=="http://twitpic" || substr($link,0,13)=="http://tumblr"
         || substr($link,0,13)=="http://mypict" || substr($link,0,14)=="http://instagr"  )
         {
          array_push($imgtweets,"$tweet");
          }
       }
      }
     }
       print_r($imgtweets);

?>  


basically you can replace all your code with something like

$hosts  = "lockerz|yfrog|twitpic|etc";
$regexp = "~http://($hosts)~";

$img_tweets = preg_grep($regexp, $all_tweets);


Rather than using substr you could use an array and str_replace

 $urls = array(*****your urls*****);
 foreach($tweets as $tweet) 
 {
   str_replace($urls, '', $tweet, $count);
   if ($count)
   {
      array_push($imgtweets,"$tweet");
   }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜