Here’s the problem I have, I’m trying to separate News by categories, I have the following txt file (which includes all of the news divided by
<item></item>
Here is a set of 4 News, on my actual file I have thousands.
<item>
Title: News from Washington
Author: John Doe
Category: New Laws
Body: News content...
</item>
<item>
Title: News from Texas
Author: General Lee
Category: Road Accidents
Body: News content/
</item>
<item>
Title: News from Georgia
Author: Marcus Smith
Category: Street Food
Body: News content
</item>
<item>
Title: News from Illinois
Author: Robert Simpson
Category: School Projects
Body: News content
</item>
I have the following coding:
//I get the content from the news file:
$news = file_get_contents("news.txt");
//Then I create the following variables to get each set of news from the news variable:
$regexp = '@<item>(.*?)</item>@msi';
what I want to do from here is that in case that I just want to get a file with News which includes just the “Street Food” as a category and to dismiss/ignore the rest of the other News with different categories.
e.g.
My result from the above example will be a file which only includes this item:
<item>
Title: News from Georgia
Author: Marcus Smith
Category: Street Food
Body: News content
</item>
I tried using a preg_match_all and a foreach function to get a set of news with certain category with no luck.
What do you suggest to accomplish this? or if you could provide me with an example that’ll be great.
Thanks in advance!
You can try
Output
You can also
Rewrite The XMLusing the followingThis would return
Reading new format easier
Re Write Function