2011-09-27 32 views
0

这是我在学习几周后第一次尝试php脚本。所以基本上我正在解析一个XML博客提要。首先,如果获取博客发布日期,并且如果它匹配今天的日期基于洛杉矶时间zome它将进一步,并获得当天的博客文章网址。否则它将返回“不匹配”。一旦发现今天的博客帖子,如果将收集每篇文章的网址。现在这里是我的问题,无论我做什么,它总是返回没有匹配的代码,我正在寻找。我认为它不检查每个网址,但由于我的知识是有限的,我可以证明。我试图用regex搜索每个帖子的数字字符串,如果发现我想要回显该字符串并结束脚本,或者没有找到代码,则回显一条消息,指出该消息并结束脚本。此外,如果你找到一个更好的方法或重写我的代码更有效的方式来清理了一下我欢迎太使用PHP结束循环,直到找到匹配

/*------------------------------------------------ 
//XML File and Parsing 
------------------------------------------------*/ 
//XML document 
$rss = simplexml_load_file($xmlfeed); 
//Blog Dates 
$blogdate = $rss->channel->item; 
//Blog URLS 
$blogurl = $rss->channel->item; 

/*------------------------------------------------ 
//Date Variables 
------------------------------------------------*/ 
//Original date format: Mon, 26 Sep 2011 22:00:08 +0000 
$post_date = $date->pubDate; 
//Original date format: September 26 2011 
$todays_date = date("F j Y"); 
$timezone = new DateTimeZone('America/Los_Angeles'); 
//Format blog post dates into PDT 
$date1 = new DateTime($post_date); 
$date1->setTimeZone($timezone); 
//Output date: September 26 2011 
$post_date_final = $date1->format("F j Y"); 
//Format server date into PDT 
$date2 = new DateTime($todays_date); 
$date2->setTimeZone($timezone); 
//Output date: September 26 2011 
$todays_date_final = $date2->format("F j Y"); 
echo $post_date; 
/*------------------------------------------------ 
//Checking and Looping 
------------------------------------------------*/ 
//Looping through blog dates for a mtach 
foreach ($blogdate as $date) { 
    //If dates match continue to gather URLs 
    if ($post_date_final == $todays_date_final) { 
     foreach ($blogurl as $url) { 
      $postone = $url->guid[0]; 
      $posttwo = $url->guid[1]; 
      $postthree = $url->guid[2]; 
      $postfour = $url->guid[3]; 
      $postfive = $url->guid[4]; 
      $postsix = $url->guid[5]; 
      $go = array($postone, $posttwo, $postthree, $postfour, $postfive, $postsix); 

      foreach ($go as $stop){ 
       $html = file_get_contents($stop); 
       preg_match('/cd=\b[0-9]{8,15}\b/', $html, $match); 
       $regex_match = $match[0]; 

       if (isset($regex_match)) { 
        echo $regex_match; 
       }else{ 
        echo "no match"; 
        exit; 
       } 
      } 
     } 
    }else{ 
     echo "no match"; 
     exit; 
    } 
} 

回答

1

我只迅速掠过你的代码,但我看到了这条线,你可能想要改变:

if(!isset($regex_match)) 

这就是说,如果$ regex_match没有设置,那么echo $ regex_match。意思是如果你试图在它没有发现的时候回应它。

试着取出那个!并看看是否有帮助。

+0

谢谢我看到了,并进行了更正,但我仍然“不匹配”。 – bammab

+0

@bammab向我们显示$ html变量的几行,其中'cd = NUM​​BER'存在 – tttony