How to remove leading white space inside xml file in php?

Member

by jasen , in category: PHP General , 9 months ago

How to remove leading white space inside xml file in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 5 months ago

@jasen 

You can remove leading white space inside an XML file in PHP using the following code snippet:

1
2
3
$xmlString = file_get_contents('your_file.xml');
$xmlString = preg_replace('/^s+/m', '', $xmlString);
file_put_contents('your_file.xml', $xmlString);


This code reads the content of the XML file into a string, uses a regular expression to remove leading white space from each line (using the multi-line modifier 'm'), and then writes the modified content back to the XML file.