php - Is there any faster/better way instead of using preg_match in the following code? -
possible duplicate:
how can edit code echo data of child's element search term found in, in xmlreader?
this code finds if there string 2004 in <date_iso></date_iso>
, if so, echo data specific element search string found.
i wondering if best/fastest approach because main concern speed , xml file huge. thank ideas.
this sample of xml
<entry id="4406"> <id>4406</id> <title>book @ 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 code
<?php $books = simplexml_load_file('planet.xml'); $search = '2004'; foreach ($books->entry $entry) { if (preg_match('/' . preg_quote($search) . '/i', $entry->date_iso)) { echo $entry->dc_date; } } ?>
this approach
<?php $books = simplexml_load_file('planet.xml'); $search = '2004'; $regex = '/' . preg_quote($search) . '/i'; foreach ($books->entry $entry) { if (preg_match($regex, $entry->date_iso)) { echo $entry->dc_date; } } ?>
if main concern speed, shouldn't use simplexml or other dom-based xml parsing this; use sax-based parser. furthermore, don't use preg_match if want simple substring matching (use strpos).
if speed isn't concern being idiomatic is, use xpath 2.0 implementation (don't know if there 1 php) or other xpath-based regex matching things - quick google shows exslt options, or simpler xpath 1.0-based string matching options.
Comments
Post a Comment