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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:51:38+00:00 2026-06-13T14:51:38+00:00

I have a script that appends XML data to the end of an XML

  • 0

I have a script that appends XML data to the end of an XML file via PHP. The only problem is that after each new line of XML I add via the PHP script, an extra line (whitespace) is created. Is there a way to remove the whitespace from the XML file with PHP without loosing the neatly formated XML file? Here is my PHP code that writes to the XML file:

<?php

function formatXmlString($xml) {  

  // add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
  $xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);

  // now indent the tags
  $token      = strtok($xml, "\n");
  $result     = ''; // holds formatted version as it is built
  $pad        = 0; // initial indent
  $matches    = array(); // returns from preg_matches()

  // scan each line and adjust indent based on opening/closing tags
  while ($token !== false) : 

  // test for the various tag states

 // 1. open and closing tags on same line - no change
 if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) : 
   $indent=0;
 // 2. closing tag - outdent now
 elseif (preg_match('/^<\/\w/', $token, $matches)) :
   $pad=0;
 // 3. opening tag - don't pad this one, only subsequent tags
 elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
   $indent=4;
 // 4. no indentation needed
 else :
   $indent = 0; 
 endif;

 // pad the line with the required number of leading spaces
 $line    = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
 $result .= $line . "\n"; // add to the cumulative result, with linefeed
 $token   = strtok("\n"); // get the next token
 $pad    += $indent; // update the pad size for subsequent lines    
 endwhile; 

return $result;
}

function append_xml($file, $content, $sibling, $single = false) {
    $doc = file_get_contents($file);
    if ($single) {
        $pos = strrpos($doc, "<$sibling");
        $pos = strpos($doc, ">", $pos) + 1;
    }
    else {
       $pos = strrpos($doc, "</$sibling>") + strlen("</$sibling>");
    }
    return file_put_contents($file, substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos));
}  



$content = "<product><id>3</id><name>Product 3</name><price>63.00</price></product>";
append_xml('prudcts.xml', formatXmlString($content), 'url');  

?>
  • 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-13T14:51:39+00:00Added an answer on June 13, 2026 at 2:51 pm

    Do not just put all in one line and you’re more flexible:

     return file_put_contents($file, substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos));
    

    Instead (suggestion):

     $buffer = substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos);
     $buffer = rtrim($buffer);
     return file_put_contents($file, $buffer);
    

    P.S: Using DomDocument might be more straight forward and save for XML processing then the string functions.

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

Sidebar

Related Questions

So, say you have a php post script that appends whatever you pass to
I have a script that submits data via an ajax POST. It is called
I have a python script that parses an XML file that contains part information
I have an xml file with a node that contains some c# code. <Script
I have an Jquery script that loads an XML file and spits out the
I have a script that reads in a large data file 3GB I don't
I have a JS script that appends to the HTML page the pairs: input
I have script that reads remote file content and writes it to local server.
I have a script that will write a given string to a text file...
Hi all I have one problem. I am connecting QT with php script..Everything works

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.