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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:02:27+00:00 2026-05-15T13:02:27+00:00

For example, we have this xml: <x> <y>some text</y> <y>[ID] hello</y> <y>world [/ID]</y> <y>some

  • 0

For example, we have this xml:

<x>
    <y>some text</y>
    <y>[ID] hello</y>
    <y>world [/ID]</y>
    <y>some text</y>
    <y>some text</y>
</x>

and we need to remove words “[ID]”, “[/ID]” and text between them (which we don’t know, when parsing), of course without damage xml formatting.

The only solution i can think is that:

  1. Find in xml the text by using regex, for example: "/\[ID\].*?\[\/ID\]/". In our case, result will be "[ID]hello</y><y>world[/ID]"

  2. In result from prev step we need to find text without xml-tags by using this regex:
    "/(?<=^|>)[^><]+?(?=<|$)/", and delete this text. The result will be "</y><y>"

  3. Made changes in original xml by doing smth like this:

    str_replace($step1string,$step2string,$xml);

is this correct way to do this?
I just think that this “str_replace”‘s things it’s not best way to edit xml, so maybe you know better solution?

  • 1 1 Answer
  • 1 View
  • 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-15T13:02:28+00:00Added an answer on May 15, 2026 at 1:02 pm

    Removing the specific string is simple:

    <?php
    $xml = '<x>
        <y>some text</y>
        <y>[ID] hello</y>
        <y>world [/ID]</y>
        <y>some text</y>
        <y>some text</y>
    </x>';
    
    $d = new DOMDocument();
    $d->loadXML($xml);
    $x = new DOMXPath($d);
    foreach($x->query('//text()[(contains(.,\'[ID]\') or contains(.,\'[/ID]\'))]') as $elm){
        $elm->nodeValue = preg_replace('/\[\/?ID\]/','',$elm->nodeValue);
    }
    var_dump($d->saveXML());
    ?>
    

    When just removing textnodes in a specific tag, one could alter te preg_replace to these 2:

     $elm->nodeValue = preg_replace('/\[ID\].*$/','',$elm->nodeValue);
     $elm->nodeValue = preg_replace('/^.*\[/ID\]/','',$elm->nodeValue);
    

    Resulting in for your example:

    <x>
    <y>some text</y>
    <y></y>
    <y></y>
    <y>some text</y>
    <y>some text</y>
    </x>
    

    However, removing tags in between without damaging well formed XML is quite tricky. Before venturing into lot of DOM actions, how would you like to handle:

    An [/ID] higher in the DOM-tree:

    <foo>[ID] foo
        <bar> lorem [/ID] ipsum </bar>
    </foo>
    

    An [/ID] lower in the DOM-tree

    <foo> foo
        <bar> lorem [ID] ipsum </bar>
        [/ID]
    </foo>
    

    And open/close spanning siblings, as per your example:

    <foo> foo
        <bar> lorem [ID] ipsum </bar>
        <bar> lorem [/ID] ipsum </bar>
    </foo>
    

    And a real dealbreaker of a question: is nesting possible, is that nesting well formed, and what should it do?

    <foo> foo
        <bar> lo  [ID] rem [ID] ipsum </bar>
        <bar> lorem [/ID] ipsum </bar>
        [/ID]
    </foo>
    

    Without further knowledge how these case should be handled there is no real answer.


    Edit, well futher information was given, the actual, fail-safe solution (i.e.: parse XML, don’t use regexes) seems kind of long, but will work in 99.99% of cases (personal typos and brainfarts excluded of course 🙂 ):

    <?php
    $xml = '<x>
        <y>some text</y>
        <y>
          <a> something </a>
          well [ID] hello
          <a> and then some</a>
        </y>
        <y>some text</y>
        <x>
          world
          <a> also </a>
            foobar [/ID] something
          <a> these nodes </a>
        </x>
        <y>some text</y>
        <y>some text</y>
    </x>';
    echo $xml;
    $d = new DOMDocument();
    $d->loadXML($xml);
    $x = new DOMXPath($d);
    foreach($x->query('//text()[contains(.,\'[ID]\')]') as $elm){
            //if this node also contains [/ID], replace and be done:
            if(($startpos = strpos($elm->nodeValue,'[ID]'))!==false && $endpos = strpos($elm->nodeValue,'[/ID]',$startpos)){
                    $elm->replaceData($startpos, $endpos-$startpos + 5,'');
                    var_dump($d->saveXML($elm));
                    continue;
            }
            //delete all siblings of this textnode not being text and having [/ID]
            while($elm->nextSibling){
                    if(!($elm->nextSibling instanceof DOMTEXT) || ($pos =strpos($elm->nodeValue,'[/ID]'))===false){
                            $elm->parentNode->removeChild($elm->nextSibling);
                    } else {
                            //id found in same element, replace and go to next [ID]
                            $elm->parentNode->appendChild(new DOMTExt(substr($elm->nextSibling->nodeValue,$pos+5)));
                            $elm->parentNode->removeChild($elm->nextSibling);
                            continue 2;
                    }
            }
            //siblings of textnode deleted, string truncated to before [ID], now let's delete intermediate nodes
            while($sibling = $elm->parentNode->nextSibling){ // in case of example: other <y> elements:
                    //loop though childnodes and search a textnode with [/ID]
                    while($child = $sibling->firstChild){
                            //delete if not a textnode
                            if(!($child instanceof DOMText)){
                                    $sibling->removeChild($child);
                                    continue;
                            }
                            //we have text, check for [/ID]
                            if(($pos = strpos($child->nodeValue,'[/ID]'))!==false){
                                    //add remaining text in textnode:
                                    $elm->appendData(substr($child->nodeValue,$pos+5));
                                    //remove current textnode with match:
                                    $sibling->removeChild($child);
                                    //sanity check: [ID] was in <y>, is [/ID]?
                                    if($sibling->tagName!= $elm->parentNode->tagname){
                                            trigger_error('[/ID] found in other tag then [/ID]: '.$sibling->tagName.'<>'.$elm->parentNode->tagName, E_USER_NOTICE);
                                    }
                                    //add remaining childs of sibling to parent of [ID]:
                                    while($sibling->firstChild){
                                            $elm->parentNode->appendChild($sibling->firstChild);
                                    }
                                    //delete the sibling that was found to hold [/ID]
                                    $sibling->parentNode->removeChild($sibling);
                                    //done: end both whiles
                                    break 2;
                            }
                            //textnode, but no [/ID], so remove:
                            $sibling->removeChild($child);
                    }
                    //no child, no text, so no [/ID], remove:
                    $elm->parentNode->parentNode->removeChild($sibling);
            }
    }
    var_dump($d->saveXML());
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document which contains the following example extract: <p> Some text
For example i have this xml. I need to get value of parameter val
I have a very simple XML datasource structured like this: <datasource> <row> <column>Some text
I have an xml which contains some keywords. The format of the xml this:
I have some XML thus <root> <TemplateNode> <Heading> <ID>1</ID> <Name>HeadingNodeTest</Name> <Order>1</Order> <Text>This is a
I have a xml file for example, <title> hello <name> hi </name> <street> id
I have some XML like this: <root> <do-not-sort> <z/> <y/> </do-not-sort> <sortable> <e f=fog
I have a little script that replace some text, that come from a xml
I have some text that is being read in from the database. This text
I have an XML file containing some scientific text that I want to display

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.