这是我学习了几周后第一次尝试编写php脚本。基本上,我正在解析一个XML博客提要。首先,它获取博客发布日期,如果它匹配基于洛杉矶时区的今天的日期,它将进一步获取当天当前博客发布的url。否则它将返回"no match"。一旦找到今天的博客文章,它将收集每个帖子的url。现在这是我的问题,无论我做什么,它总是返回不匹配的代码,我正在寻找。我假设它没有检查每个url,但由于我的知识有限,我可以肯定。我正试图用正则表达式搜索每个帖子的数字字符串,如果发现我想回显该字符串并结束脚本,或者如果没有找到代码回显消息,说明并结束脚本。此外,如果你找到一个更好的方法或更有效的方式重写我的代码来清理它,我也欢迎
/*------------------------------------------------
//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;
}
}
我只是快速浏览了一下你的代码,但我看到了这一行你可能想要修改:
if(!isset($regex_match))
它的意思是,如果$regex_match没有设置,那么回显$regex_match。意思是如果你试图在它没有找到任何东西的时候回显它。
试试把它拿出来!看看有没有帮助