removing new lines except in

后端 未结 3 584
温柔的废话
温柔的废话 2021-01-14 19:24

I want to remove new lines from some html (with php) except in

 tags where whitespace is obviously important.

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 20:17

    If the html is well formed, you can rely on the fact that

     tags aren't allowed to be nested. Make two passes: First you split the input into block of pre tags and everything else. You can use a regular expression for this task. Then you strip new lines from each non-pre block, and finally join them all back together.

    Note that most html isn't well formed, so this approach may have some limits to where you can use it.

提交回复
热议问题