2011-09-27 66 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