Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:16:55+00:00 2026-06-06T21:16:55+00:00

I am looping over a twitter feed and everything in my code works fine,

  • 0

I am looping over a twitter feed and everything in my code works fine, but I am struggling with one thing. The if statement towards the bottom returns all messages that have a date for today. Then based on that I have a regex pattern I am trying to test for. If its found I state “found it” and if not I state “not found”. What I need help with is the not found part. Rather than stating “not found” for each message how can I make it so that it will loop over the messages and if found state “found it” and stop everything, but if not found pass over the message and then once all the looping is done state “not found” only once.

$dom = new DOMDocument();
@$dom->loadHTMLFile('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=google');
$xml = simplexml_import_dom($dom);
$twitter = $xml->xpath("//item");

foreach ($twitter as $item) {
    $timezone = new DateTimeZone('America/Los_Angeles');
    $date = new DateTime($item->pubdate);
    $date->setTimeZone($timezone);
    $twitter_date = $date->format("F j Y");
    $todays_date = date("F j Y");

    if ($twitter_date == $todays_date) {
        $text = $item->title;
        $pattern = '/\s\d\s\d/i';
        if (preg_match($pattern, $text, $codes)) {
            if (isset($codes[0])) {
                echo 'found it!';
            }
        } else {
            echo 'not found';
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:16:57+00:00Added an answer on June 6, 2026 at 9:16 pm

    You can keep a flag variable $found to track whether it was found, and then break out of the loop when one is found. Then after your foreach loop, check the value of $found.

    $found = false;
    foreach ($twitter as $item) {
        // ...
        if ($twitter_date == $todays_date) {
            $text = $item->title;
            $pattern = '/\s\d\s\d/i';
            if (preg_match($pattern, $text, $codes)) {
                if (isset($codes[0])) {
                    $found = true;
                    echo 'found it!';
                    break;
                } 
            }
        }
    }
    if (!$found) {
        echo 'not found';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code does destroy records as intended, but the callback is inherited from one
I am reading an ATOM feed with jQuery and looping over the results. While
I'm new to Postgresql and struggling to build a function for looping over a
For example if looping over @feed, which renders a partial, you see the following
A dictionary cannot be changed if we are looping over it in .Net. But
I'm trying to read a file of regexes, looping over them and filtering them
I have HTML table. I'm looping through table and iterate over each row whose
I have been looking over the internet for a while about this, but it
I have been looking over this code for the past hour, I cant see
I was looking over the code for qunit. My question is why would you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.