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?

4
  • See Tidy Commented May 8, 2014 at 19:00
  • @AlexHowansky, thnx but it requires additional lib install and specific setup , the code I am using is intended for a lot of people and there is no way that their environment would match. Commented May 8, 2014 at 19:34
  • 1
    Wrap your code with if (class_exists('Tidy')) { ... } Commented May 8, 2014 at 19:51
  • Found a solution here stackoverflow.com/questions/3616540/format-xml-string Commented May 10, 2014 at 15:13

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

I posted the link already, here again prntscr.com/3hegmb, your suggestion is removing all spaces from xml file. I want to clean the formating up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.