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

  • Home
  • SEARCH
  • 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 8674117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:37:39+00:00 2026-06-12T19:37:39+00:00

Here’s the problem I have, I’m trying to separate News by categories, I have

  • 0

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!

  • 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-06-12T19:37:40+00:00Added an answer on June 12, 2026 at 7:37 pm

    You can try

    $final = array();
    $filename = "log.txt";
    $news = simplexml_load_file($filename);
    
    foreach ( $news as $item ) {
        $item = trim($item);
        $content = array();
        foreach ( explode("\n", $item) as $info ) {
            list($title, $data) = explode(":", $info);
            $content[trim($title)] = $data;
        }
        $final[trim($content['Category'])][] = $content;
    }
    
    
    #Remove Street Food
    unset($final['Street Food']);
    
    #Output The Rest 
    var_dump($final);
    

    Output

        array
      'New Laws' => 
        array
          0 => 
            array
              'Title' => string ' News from Washington' (length=21)
              'Author' => string ' John Doe' (length=9)
              'Category' => string ' New Laws' (length=9)
              'Body' => string ' News content...' (length=16)
      'Road Accidents' => 
        array
          0 => 
            array
              'Title' => string ' News from Texas' (length=16)
              'Author' => string ' General Lee' (length=12)
              'Category' => string ' Road Accidents' (length=15)
              'Body' => string ' News content/' (length=14)
      'School Projects' => 
        array
          0 => 
            array
              'Title' => string ' News from Illinois' (length=19)
              'Author' => string ' Robert Simpson' (length=15)
              'Category' => string ' School Projects' (length=16)
              'Body' => string ' News content' (length=13)
    

    You can also Rewrite The XML using the following

    #Rewrite the array to new XML Fromat
    rewriteToXML($final,"log.xml");
    

    This would return

    <?xml version="1.0"?>
    <items>
        <item>
            <Title> News from Washington</Title>
            <Author> John Doe</Author>
            <Category> New Laws</Category>
            <Body> News content...</Body>
        </item>
        <item>
            <Title> News from Texas</Title>
            <Author> General Lee</Author>
            <Category> Road Accidents</Category>
            <Body> News content/</Body>
        </item>
        <item>
            <Title> News from Illinois</Title>
            <Author> Robert Simpson</Author>
            <Category> School Projects</Category>
            <Body> News content</Body>
        </item>
    </items>
    

    Reading new format easier

    $final = array();
    $filename = "log.xml";
    $news = simplexml_load_file($filename);
    
    foreach ( $news as $item ) {
        #Check if not Street Food
        if(trim($item->Category) != 'Street Food')
                $final[trim($item->Category)][] = (array) $item;
    }
    
    #Output The Rest
    var_dump($final);
    

    Re Write Function

    function rewriteToXML($array, $fileName = null) {
        $xml = new SimpleXMLElement("<items />");
        foreach ( $array as $key => $item ) {
            $child = $xml->addChild("item");
            foreach ( $item as $list ) {
                foreach ( $list as $title => $data ) 
                {
                    $child->addChild($title, $data);
                }
            }
        }
        $xml->asXML($fileName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is my problem.. I have the option to create(add) two new input fields,
Here are the tables I have: Table A which has entries with item and
Here's the code I have. It works. The only problem is that the first
Here's the problem....I have three components...A Page that contains a User Control and a
Here is what I am trying to achieve in PHP: I have this string:
Here is the scenario: I have several CIFS partitions which AD-based users can mount
Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I have just tried to save a simple *.rtf file with some websites and

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.