문제

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?

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top