使用 php 结束循环,直到找到匹配项
这是我学习几周后第一次尝试 php 脚本。基本上我正在解析 XML 博客提要。首先,如果获取博客文章日期,并且如果它与基于洛杉矶时区的今天日期匹配,它将进一步获取当天的当前博客文章网址。否则它将返回“不匹配”。找到今天的博客文章后,如果会收集每篇文章的网址。现在这是我的问题,无论我做什么,它总是返回与我正在寻找的代码不匹配的内容。我认为它不会检查每个网址,但由于我的知识有限,我可以肯定。我正在尝试使用正则表达式在每个帖子中搜索数字字符串,如果找到,我想回显该字符串并结束脚本,或者如果没有找到代码,则回显一条消息说明该字符串并结束脚本。另外,如果您找到更好的方法或更有效的方法来重写我的代码以清理它,我也欢迎
/*------------------------------------------------
//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;
}
}
This is my first attempt at a php script after a few weeks of learning. So basically I am parsing an XML blog feed. First if gets the blog post date and if it matches today's date based on Los Angeles time zome it will go further and get the current blog post urls for the day. Else it will return "no match". Upon finding blog posts for today if will gather the url of each post. Now here is my issue no matter what i do it always returns no match on the code i am looking for. I assume its not checking each url but since my knowledge is limited I can be certian. I am trying to search each post for a numerical string with regex and if its found I want to echo that string and end the script or if there is no code found echo a message stating that and also end the script. Also if you found a better way or a more efficient way of rewriting my code to clean it up a bit i welcome that too
/*------------------------------------------------
//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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是快速浏览了你的代码,但我看到了你可能想要更改的这一行:
它是说,如果 $regex_match 未设置,则 echo $regex_match。意思是如果您在未找到某些内容时尝试回显它。
尝试把它拿出来!看看是否有帮助。
I've only quickly skimmed over your code but I saw this line which you might want to change:
It is saying, if $regex_match is not set then echo $regex_match. Meaning if you are trying to echo it when it has not found something.
Try taking out that ! and see if that helps.