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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:14:45+00:00 2026-05-24T09:14:45+00:00

So I am trying to parse an XML file and display first 150 words

  • 0

So I am trying to parse an XML file and display first 150 words of an article with READ MORE link. It doesn’t exactly parse 150 words though. I am also not sure how to make it so it does not parse IMG tag code, etc… the code is below

    // Script displays 3 most recent blog posts from blog.pinchit.com (blog..pinchit.com/api/read)
    // The entries on homepage show the first 150 words of description and "READ MORE" link

    // PART 1 - PARSING

    // if it was a JSON file
    //  $string=file_get_contents("http://blog.pinchit.com/api/read");
    //  $json_a=json_decode($string,true);
    //  var_export($json_a);


    // XML Parsing
    $file = "http://blog.pinchit.com/api/read";
    $posts_to_display = 3;
    $posts = array();

    // get all the file nodes
    if(!$xml=simplexml_load_file($file)){
        trigger_error('Error reading XML file',E_USER_ERROR);
    }

    // counter for posts member array
    $counter = 0;

    // Accessing elements within an XML document that contain characters not permitted under PHP's naming convention 
    // (e.g. the hyphen) can be accomplished  by encapsulating the element name within braces and the apostrophe.

    foreach($xml->posts->post as $post){

        //post's title
        $posts[$counter]['title'] = $post->{'regular-title'};

        // post's full body 
        $posts[$counter]['body'] = $post->{'regular-body'};

        // post's body's first 150 words 
        //for some reason, I am not sure if it's exactly 150 
        $posts[$counter]['preview'] = substr($posts[$counter]['body'], 0, 150);

        //strip all the html tags so it doesn't mess up the page
        $posts[$counter]['preview'] = strip_tags($posts[$counter]['preview']);


        //post's id
        $posts[$counter]['id'] = $post->attributes()->id;


        $posts_to_display--;
        $counter++;
        //exit the for loop after we parse out all the articles that we want
        if ($posts_to_display == 0 ) break;
    }

    // Displays all of the posts

    foreach($posts as $post){

        echo "<b>" . $post['title'] . "</b>";
        echo "<br/>";
        echo $post['preview'];
        echo " <a href='http://blog.pinchit.com/post/" . $post[id] . "'>Read More</a>";
        echo "<br/><br/>";

    } 

Here are how results look now.

Editor’s Pick: Club Sportiva
Nothing makes you feel as totally free and in control as a day behind the wheel of a sleek, sophisticated, sexy sports car. It’s no surprise Read More

Pinchy Drinks & Rocks: The Hotel Utah Saloon
Hotel Utah Read More

Monday Menu: Spicy Grapefruit, Paprika, Creamsicles
Feeling summery and savory today, and we have to admit it took a lot to resist the urge to make this an all appetizers, all desserts, or all drinks Read More

  • 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-05-24T09:14:45+00:00Added an answer on May 24, 2026 at 9:14 am

    The HTML tags are counting against your character total. Strip the tags out first, then take your preview sample:

    $preview = strip_tags($posts[$counter]['body']);
    $posts[$counter]['preview'] = substr($preview, 0, 150).'...';
    

    Also, one usually adds an ellipse (“…”) to the end of truncated text to indicate that it continues.

    Note that this has the potential disadvantage of removing tags you DO want, like <p> and <br>. If you want to preserve those, you can pass them as the second argument for strip_tags:

    $preview = strip_tags($posts[$counter]['body'], '<br><p>');
    $posts[$counter]['preview'] = substr($preview, 0, 150).'...';
    

    BUT, be forewarned that XML-style tags might throw this off (<br />). If you’re dealing with XML/HTML mixed, you might need to elevate your tag filtering using something like htmLawed, but the concept remains the same – get rid of the HTML before you truncate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse an xml file output from googletest to display the results
I'm trying to parse objects to XML in Delphi, so I read about calling
I'm trying to use the lxml library to parse an XML file...what I want
I am trying to parse a xml file that i have (using javascript DOM).
I am trying to parse one XML file that contains some unicode characters.I tried
I'm trying to parse an xml file from a website. Let's say the website
I'm getting this error (see title) while trying to parse an XML file in
Hey guys. I'm trying to parse xml file in order to extract some data
I am trying to access an online XML file and display it's contents within
While trying to parse an xml file into table format, jQuery keeps closing my

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.