0

I am removing several code lines from an xml file with preg_replace and str_replace

preg_replace('/(<!DOCTYPE(.*?)<fields )/s', '<fields ', $readSettings);

after the replace is done I am left with an empty spaces where the lines were. http://prntscr.com/3hegmb line 4 and 5 Is there any way to reformat the file once I am done or to remove the line and its space completely?

Benn
  • 4,840
  • 8
  • 65
  • 106

1 Answers1

1

Not 100% sure what you are asking. Can you post an example of the HTML you get after the RegEx?

This will remove excess white space.

preg_replace( '~\s{2,}~', '', $str );

You could also look at PHP DOM (PHP Manual). Load the HTML into that and then set preserveWhiteSpace and formatOutput to false.

Addition:

preg_replace( '~\s+\n+\s+\n+~', "\n", $str );

Will do it I think. But as mentioned you can do this with PHP DOM, and it will be more flexible.

$doc = new DOMDocument();
$doc->loadHTML( $html );
$doc->preserveWhiteSpace = true; // Or false?
$doc->formatOutput = true;
echo $doc->saveHTML();
Kohjah Breese
  • 4,008
  • 6
  • 32
  • 48
  • I posted the link already, here again http://prntscr.com/3hegmb, your suggestion is removing all spaces from xml file. I want to clean the formating up – Benn May 10 '14 at 14:45