using php to end a loop until match is found
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/Lo开发者_StackOverflow中文版s_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;
}
}
I've only quickly skimmed over your code but I saw this line which you might want to change:
if(!isset($regex_match))
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.
精彩评论