我想從XML文件中刪除所有換行符和回車符,以便每個標籤都適合一行。正則表達式:從部分字符串中刪除換行符(PHP)
XML源例如:
<resources>
<resource>
<id>001</id>
<name>Resource name 1</name>
<desc>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh magna, fermentum et pretium vel, malesuada sit amet dolor. Morbi dictum, nunc sed interdum facilisis, ligula enim pharetra tortor, at egestas urna massa non nulla.</desc>
</resource>
<resource>
<id>002</id>
<name>Resource name 2</name>
<desc>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh magna, fermentum et pretium vel, malesuada sit amet dolor. Morbi dictum, nunc sed interdum facilisis, ligula enim pharetra tortor, at egestas urna massa non nulla.
</desc>
</resource>
<resource>
<id>003</id>
<name>Resource name 3</name>
<desc>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh magna, fermentum et pretium vel, malesuada sit amet dolor.
Morbi dictum, nunc sed interdum facilisis, ligula enim pharetra tortor, at egestas urna massa non nulla.
</desc>
</resource>
</resources>
我拿吧:
$pattern = "#(\t\t<[^>]*>[^<>]*)[\r\n]+([^<>]*</.*>)#";
$replacement = "$1$2";
$data = preg_replace($pattern, $replacement, $data);
這種模式校正第二資源,並把它重新站線。但是,它不能糾正第三個資源的兩個換行符,它只能糾正一個。結果是這樣的:
<resources>
<resource>
<id>001</id>
<name>Resource name 1</name>
<desc>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh magna, fermentum et pretium vel, malesuada sit amet dolor. Morbi dictum, nunc sed interdum facilisis, ligula enim pharetra tortor, at egestas urna massa non nulla.</desc>
</resource>
<resource>
<id>002</id>
<name>Resource name 2</name>
<desc>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh magna, fermentum et pretium vel, malesuada sit amet dolor. Morbi dictum, nunc sed interdum facilisis, ligula enim pharetra tortor, at egestas urna massa non nulla.</desc>
</resource>
<resource>
<id>003</id>
<name>Resource name 3</name>
<desc>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh magna, fermentum et pretium vel, malesuada sit amet dolor.
Morbi dictum, nunc sed interdum facilisis, ligula enim pharetra tortor, at egestas urna massa non nulla.</desc>
</resource>
</resources>
我的模式有什麼問題?