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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:50:32+00:00 2026-05-27T17:50:32+00:00

I have some XML chunk returned by DOMDocument::saveXML() . It’s already pretty indented, with

  • 0

I have some XML chunk returned by DOMDocument::saveXML(). It’s already pretty indented, with two spaces per level, like so:

<?xml version="1.0"?>
<root>
  <error>
    <a>eee</a>
    <b>sd</b>
  </error>
</root>

As it’s not possible to configure DOMDocument (AFAIK) about the indentation character(s), I thought it’s possible to run a regular expression and change the indentation by replacing all two-space-pairs into a tab. This can be done with a callback function (Demo):

$xml_string = $doc->saveXML();
function callback($m)
{
    $spaces = strlen($m[0]);
    $tabs = $spaces / 2;
    return str_repeat("\t", $tabs);
}
$xml_string = preg_replace_callback('/^(?:[ ]{2})+/um', 'callback', $xml_string);

I’m now wondering if it’s possible to do this w/o a callback function (and without the e-modifier (EVAL)). Any regex wizards with an idea?

  • 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-27T17:50:33+00:00Added an answer on May 27, 2026 at 5:50 pm

    You can use \G:

    preg_replace('/^  |\G  /m', "\t", $string);
    

    Did some benchmarks and got following results on Win32 with PHP 5.2 and 5.4:

    >php -v
    PHP 5.2.17 (cli) (built: Jan  6 2011 17:28:41)
    Copyright (c) 1997-2010 The PHP Group
    Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
    
    >php -n test.php
    XML length: 21100
    Iterations: 1000
    callback: 2.3627231121063
    \G:       1.4221360683441
    while:    3.0971200466156
    /e:       7.8781840801239
    
    
    >php -v
    PHP 5.4.0 (cli) (built: Feb 29 2012 19:06:50)
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    
    >php -n test.php
    XML length: 21100
    Iterations: 1000
    callback: 1.3771259784698
    \G:       1.4414191246033
    while:    2.7389969825745
    /e:       5.5516891479492
    

    Surprising that callback is faster than than \G in PHP 5.4 (altho that seems to depend on the data, \G is faster in some other cases).

    For \G /^ |\G /m is used, and is a bit faster than /(?:^|\G) /m.
    /(?>^|\G) /m is even slower than /(?:^|\G) /m.
    /u, /S, /X switches didn’t affect \G performance noticeably.

    The while replace is fastest if depth is low (up to about 4 indentations, 8 spaces, in my test), but then gets slower as the depth increases.

    The following code was used:

    <?php
    
    $base_iter = 1000;
    
    $xml_string = str_repeat(<<<_STR_
    <?xml version="1.0"?>
    <root>
      <error>
        <a>  eee  </a>
        <b>  sd    </b>         
        <c>
                deep
                    deeper  still
                        deepest  !
        </c>
      </error>
    </root>
    _STR_
    , 100);
    
    
    //*** while ***
    
    $re = '%# Match leading spaces following leading tabs.
        ^                     # Anchor to start of line.
        (\t*)                 # $1: Preserve any/all leading tabs.
        [ ]{2}                # Match "n" spaces.
        %mx';
    
    function conv_indent_while($xml_string) {
        global $re;
    
        while(preg_match($re, $xml_string))
            $xml_string = preg_replace($re, "$1\t", $xml_string);
    
        return $xml_string;
    }
    
    
    //*** \G ****
    
    function conv_indent_g($string){
        return preg_replace('/^  |\G  /m', "\t", $string);
    }
    
    
    //*** callback ***
    
    function callback($m)
    {
        $spaces = strlen($m[0]);
        $tabs = $spaces / 2;
        return str_repeat("\t", $tabs);
    }
    function conv_indent_callback($str){
        return preg_replace_callback('/^(?:[ ]{2})+/m', 'callback', $str);
    }
    
    
    //*** callback /e *** 
    
    function conv_indent_e($str){
        return preg_replace('/^(?:  )+/me', 'str_repeat("\t", strlen("$0")/2)', $str);
    }
    
    
    
    //*** tests
    
    function test2() {
        global $base_iter;
        global $xml_string;
        $t = microtime(true);
    
        for($i = 0; $i < $base_iter; ++$i){
            $s = conv_indent_while($xml_string);
            if(strlen($s) >= strlen($xml_string))
                exit("strlen invalid 2");
        }
    
        return (microtime(true) - $t);
    }
    
    function test1() {
        global $base_iter;
        global $xml_string;
        $t = microtime(true);
    
        for($i = 0; $i < $base_iter; ++$i){
            $s = conv_indent_g($xml_string);
            if(strlen($s) >= strlen($xml_string))
                exit("strlen invalid 1");
        }
    
        return (microtime(true) - $t);
    }
    
    function test0(){
        global $base_iter;
        global $xml_string;
        $t = microtime(true);
    
        for($i = 0; $i < $base_iter; ++$i){     
            $s = conv_indent_callback($xml_string);
            if(strlen($s) >= strlen($xml_string))
                exit("strlen invalid 0");
        }
    
        return (microtime(true) - $t);
    }
    
    
    function test3(){
        global $base_iter;
        global $xml_string;
        $t = microtime(true);
    
        for($i = 0; $i < $base_iter; ++$i){     
            $s = conv_indent_e($xml_string);
            if(strlen($s) >= strlen($xml_string))
                exit("strlen invalid 02");
        }
    
        return (microtime(true) - $t);
    }
    
    
    
    echo 'XML length: ' . strlen($xml_string) . "\n";
    echo 'Iterations: ' . $base_iter . "\n";
    
    echo 'callback: ' . test0() . "\n";
    echo '\G:       ' . test1() . "\n";
    echo 'while:    ' . test2() . "\n";
    echo '/e:       ' . test3() . "\n";
    
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some xml files with figure spaces in it, I need to remove
I have some xml being returned in sharepoint, Im using xslt to create the
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have some XML in an XmlDocument, and I want to display it on
i have some xml with math ml inside, and i would like to find
I have some XML which contains records and sub records, like this: <data> <record
I have some XML which I'm trying to search: <debts> <section id=24 description=YYYYY> <section
I have some xml like this: <Data> <Rows> <Row> <Field Name=title>Mr</Field> <Field Name=surname>Doe</Field> <Row>
I have some xml data contained in three files (Database.xml, Participants.xml, and ConditionTokens.xml). I
I have some xml similar to this: <?xml version=1.0 encoding=utf-8 ?> <data> <resources> <resource

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.