Jump to content

continue loop


mraza

Recommended Posts

hi i am stuck in a loop please can someone check what is miss. I wants to quit from loop and go to next title if that tile is found but cant figure out how should i go. my code:

$feed_url = "http://www.myfeed.com/feed/";
$xml = simplexml_load_file($feed_url);
foreach ($xml->channel->item as $item) { 

$ns_content = $item->children('http://purl.org/rss/1.0/modules/content/');
$title = $item->title;
// opening file which contain titles already
$content = file('file.txt');
foreach ($content as $aline) {
    if (strstr($title, $aline)) {
        $found =  true;
    } 
}
// i dont wants to print that $title which is already in file.txt so loop continue.
	if($found) {
	continue;
	}

echo $title . "<br>" ;

}

 

Problem is with that continue statement if a title is matched from file.txt it will display me nothing, i only wants to skip that title and echo other titles. thanks for any help

 

Link to comment
Share on other sites

thanks but it is not showing anything either

$found =  false;
foreach ($xml->channel->item as $item) { 
$ns_content = $item->children('http://purl.org/rss/1.0/modules/content/');
$title = $item->title;
$content = file('file.txt');
foreach ($content as $aline) {
    if (strstr($title, $aline)) {
        $found =  true;
    } 
}
	if($found){ 
	continue;
	}

	echo $title . "<br>" ;


}

it display only a blank page. if i remove continue; it shows me all titles from feed without problem. in my file.txt all titles are on a new line so are identical.

Link to comment
Share on other sites

foreach ($xml->channel->item as $item) {
   $found =  false;

 

But like I said if the titles are identical then this is easier and faster:

 

$feed_url = "http://www.myfeed.com/feed/";
$xml = simplexml_load_file($feed_url);
$content = file('file.txt');

foreach ($xml->channel->item as $item) { 
  $title = $item->title;

  if(!in_array($title, $content)) {
    echo $title;
  }
}

Link to comment
Share on other sites

hm thanks again sir but for some reason !in_array is also not working properly, if there is title in file it still display title with above code.

in my other script what i do is run the script and save that title of feed in a file here is code

$ourFileName = "file.txt";
$ourFileHandle = fopen($ourFileName, 'a') or die("can't open file");
fwrite($ourFileHandle, "$title\n");
fclose($ourFileHandle);

so it save that title in file.txt so next time when i run above script it should not read that feed again which has same title in file.txt, is there are any other thing i can try? thanks again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.