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 6169837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:55:31+00:00 2026-05-23T22:55:31+00:00

Possible Duplicate: How can I edit my code to echo the data of child's

  • 0

Possible Duplicate:
How can I edit my code to echo the data of child's element where my search term was found in, in XMLReader?

this code finds if there is the string 2004 in <date_iso></date_iso> and if it is so, I echo some data from that specific element that the search string was found.

I was wondering if this is the best/fastest approach because my main concern is speed and the XML file is huge. Thank you for your ideas.

this is a sample of the XML

<entry ID="4406">
    <id>4406</id>
    <title>Book Look Back at 2002</title>
    <link>http://www.sebastian-bergmann.de/blog/archives/33_Book_Look_Back_at_2002.html</link>
    <description></description>
    <content_encoded></content_encoded>
    <dc_date>20.1.2003, 07:11</dc_date>
    <date_iso>2003-01-20T07:11</date_iso>
    <blog_link/>
    <blog_title/>
</entry>

this is the code

<?php
$books  = simplexml_load_file('planet.xml');
$search = '2004';
foreach ($books->entry as $entry) {
    if (preg_match('/' . preg_quote($search) . '/i', $entry->date_iso)) {
        echo $entry->dc_date;
    }
}
?>

this is another approach

<?php
$books  = simplexml_load_file('planet.xml');
$search = '2004';
$regex = '/' . preg_quote($search) . '/i';
foreach ($books->entry as $entry) {
    if (preg_match($regex, $entry->date_iso)) {
        echo $entry->dc_date;
    }
}
?>
  • 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-05-23T22:55:32+00:00Added an answer on May 23, 2026 at 10:55 pm

    If your main concern is speed, you shouldn’t use simplexml or any other DOM-based xml parsing for this; use a SAX-based parser. Furthermore, don’t use preg_match if you only want to do simple substring matching (use strpos).

    If speed isn’t really your concern but being idiomatic is, use an XPath 2.0 implementation (don’t know if there is one for PHP) or do other XPath-based regex matching things – a quick google shows exslt options, or simpler xpath 1.0-based string matching options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: VS2008 Setup Project: Shared (By All Users) Application Data Files? Please can
Possible Duplicate: Member initialization of a data structure’s members EDIT: I typed the title
Possible Duplicate: php edit for Cufon font replacement I am reposting this due to
Possible Duplicate: Can I go back and edit comments on an SVN checkin? I've
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: How can I find the method that called the current method? I

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.