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 3695438
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:37:04+00:00 2026-05-19T04:37:04+00:00

Another question about PHP and XML… Is it posible to exclude children based on

  • 0

Another question about PHP and XML…

Is it posible to exclude children based on there childrens content.

See the example below:
If “title” contains the word “XTRA:” I don’t want this “movie” to be listed.

This is my PHP code:

<? $xml = simplexml_load_file("movies.xml");
foreach ($xml->movie as $movie){ ?>

<h2><? echo $movie->title ?></h2>
<p>Year: <? echo $movie->year ?></p>

<? } ?>

This is mys XML file:

<?xml version="1.0" encoding="UTF-8"?>
<movies>
    <movie>
        <title>Little Fockers</title>
    <year>2010</year>
</movie>
<movie>
    <title>Little Fockers XTRA: teaser 3</title>
    <year>2010</year>
</movie>
</movies>

The outcome of the code above is:

<h2>Little Fockers</h2>
<p>Year: 2010</p>

<h2>Little Fockers XTRA: teaser 3</h2>
<p>Year: 2010</p>

I want it to be only:

<h2>Little Fockers</h2>
<p>Year: 2010</p>
  • 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-19T04:37:05+00:00Added an answer on May 19, 2026 at 4:37 am

    There are two ways to filter nodes. The first is with XPath 1.0 (the version supported by libxml/SimpleXML)

    $movies = simplexml_load_file("movies.xml");
    
    foreach ($movies->xpath('movie[not(contains(title, "XTRA:"))]') as $movie)
    {
        echo '<h2>', $movie->title, '</h2>';
    }
    

    Here, we use the contains function, which is XPath’s equivalent to PHP’s strpos(). Unfortunately, XPath 1.0 only has a few string functions and all of them are case sensitive. So while XPath is very adapted to handle complex hierarchy, it’s not so good at dealing with string. In that case, you can fallback to a PHP solution such as:

    $movies = simplexml_load_file("movies.xml");
    
    foreach ($movies->movie as $movie)
    {
        if (stripos($movie->title, 'XTRA:') !== false)
        {
            // skip to next iteration
            continue;
        }
    
        echo '<h2>', $movie->title, '</h2>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inspired by another question asking about the missing Zip function: Why is there no
I asked another question about HTML and PHP separation as I have seen references
Another question asked about determining odd/evenness in C, and the idiomatic (x & 1)
Another poster asked about preferred syntax for infinite loops . A follow-up question: Why
This question is based on another question of mine (thankfully answered). So if in
I had posted another question earlier about deployment with Passenger. That problem turned out
this is the follow-up to my question about getting a div's content. the second
In another question I posted yesterday, I got very good advice on how a
In another question, Mark speaks highly of IDEs, saying some people still just dont
In another question on SO I answered with code like the one below 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.