Using regex on an rss feed?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
OfficialJ
Forum Newbie
Posts: 1
Joined: Fri Mar 30, 2007 9:18 pm

Using regex on an rss feed?

Post by OfficialJ »

The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to create one "master" feed that includes data from all of the sites in my network, and I am using regex to copy all of the content into a new database. I have been able to copy the titles and links without any problem, but I'm running into trouble when trying to copy the content within the description, which is set up like this:

[syntax="html"]<description>
<![CDATA[ 
Some text here with <b>some</b> <I>HTML</i> elements, [...]
]]> 
</description>
I tried using the following to just extract the content within CDATA:[/syntax]

Code: Select all

preg_match_all("/CDATA\[([^(?!\]\]\>)]*)/is",$data,$match);
But instead of stopping at the string "]]>" like I want it to, it's stopping at the first instance of "]" (I want it to stop at the entire "]]>" phrase). So my question is:

Is there a better regex to just get the description?

AND

Is there a way to include any characters up to a certain phrase, or can you only do it on an individual character basis like the string above?


The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I would use simpleXML to parse RSS - make your life much easier!

Use DOM if you don't have simpleXML
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Using regex on an rss feed?

Post by Benjamin »

Hmm, maybe this will work? Untested..

Code: Select all

preg_match_all("#<!\[CDATA\[(.*)\]\]>#is",$data,$match);
It may be too greedy but I believe it may work..
Post Reply