开发者

A regex to remove whitespace and line breaks from HTML document

I am using this regular expression to remove white space and line breaks from a HTML开发者_如何学编程 document.

However, it doesn't seem to handling line breaks very well.

preg_replace('/(?:(?<=\>)|(?<=\/\>))(\s+)(?=\<\/?)/', '', $HTML);

How can I improve the above?

I am only trying to remove spaces between beginning and end of HTML tags.


How about this regex? It's not perfect (it only handles whitespace at the beginning and end of the line) but it works for me.

$html = preg_replace('/[\t\s\n]*(<.*>)[\t\s\n]*/', '$1', $html);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜