Page 1 of 1

Creating an array based on content -between- two strings.

Posted: Mon Aug 17, 2009 6:43 am
by kykin
Hello!
For your reading convenience, I have divided this question into two versions: The short version and the long version. One is just a question with no clarification and the other has examples and things.

Short version of the question:
I want to be able to take two known (static) strings and pull out the information between those two strings without having to explode the bigger string 20 times before I'm left with just the information I want.

Long version of the question:
I'm trying to parse an XML file (specifically an RSS feed) into little blocks of information that will get dropped into a database.
Now, for the example:

Incoming RSS:

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Foods and Stuff</title>
    <link>http://www.foodexample.com</link>
    <atom:link type="application/rss+xml" rel="self" href="http://foodexample.com/user/758392/posts.rss"/>
    <description>Food Updates from Ed</description>
    <language>en-us</language>
    <ttl>40</ttl>
  <item>
    <title>Affogato at Lil Oven. Delicious.</title>
    <category>Deserts</category>
    <pubDate>Mon, 17 Aug 2009 11:07:59 +0000</pubDate>
    <link>http://foodexample.com/user/758392/3359886510</link>
  </item>
  <item>
    <title>Ice Cream at TCBY</title>
    <category>IceCream</category>
    <pubDate>Mon, 17 Aug 2009 11:07:59 +0000</pubDate>
    <link>http://foodexample.com/user/758392/3359847568</link>
  </item>
  <item>
    <title>Hamburger Steak Plate Lunch at LikeLike Drive-Inn</title>
    <category>PlateLunch</category>
    <pubDate>Mon, 17 Aug 2009 11:07:59 +0000</pubDate>
    <link>http://foodexample.com/user/758392/3359827490</link>
  </item>
 </channel>
</rss>
 
Now, I'd like to strip out everything before the first <item> tag and after the last </item> tag.. so i'm just left with:

Code: Select all

 
  <item>
    <title>Affogato at Lil Oven. Delicious.</title>
    <category>Deserts</category>
    <pubDate>Mon, 17 Aug 2009 11:07:59 +0000</pubDate>
    <link>http://foodexample.com/user/758392/3359886510</link>
  </item>
  <item>
    <title>Ice Cream at TCBY</title>
    <category>IceCream</category>
    <pubDate>Mon, 17 Aug 2009 11:07:59 +0000</pubDate>
    <link>http://foodexample.com/user/758392/3359847568</link>
  </item>
  <item>
    <title>Hamburger Steak Plate Lunch at LikeLike Drive-Inn</title>
    <category>PlateLunch</category>
    <pubDate>Mon, 17 Aug 2009 11:07:59 +0000</pubDate>
    <link>http://foodexample.com/user/758392/3359827490</link>
  </item>
 
...and then handle each block (the content between each <item> and </item> tag) separately so i can dump those do a database in each its own respective row from an array:

id // title // category // pubDate // link
45 // Affogato at Lil Oven // Deserts // Mon, 17 Aug 2009 11:07:59 +0000 // http://foodexample.com/user/758392/3359886510
46 // Ice Cream at TCBY // IceCream // Mon, 17 Aug 2009 11:07:59 +0000 // http://foodexample.com/user/758392/3359847568
47 // Hamburger Steak Plate Lunch... // Plate Lunch // Mon, 17 Aug 2009 11:07:59 +0000 // http://foodexample.com/user/758392/3359827490

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Thank you for any help you guys might have to offer. If nothing else, I'd love to be pointed in the right direction. I don't expect the full code for this to function, as I can write php fine....just can't figure out what function this is...

ps: of course, i can explode everything...but like every good explosion, i'll just end up with bits everywhere.

~kykin

Re: Creating an array based on content -between- two strings.

Posted: Mon Aug 17, 2009 7:04 am
by susrisha
I will rather use simplexml class for the same.
Here is a sample code that i have run to get your desired output.

Code: Select all

 
$root_element =simplexml_load_file('rssfile.xml');
foreach($root_element->channel->item as $child_item)
{
    echo $child_item->asXML();
    //your other attributes will for each item will
    // be accessed here..
}
 
the fiel rssfile.xml contains the same info that you will receive from rss.
If you are trying to load the same xml content from a string that you get, then use the function

Code: Select all

simplexml_load_string()