开发者

A regex to remove whitespace and line breaks from HTML document

开发者 https://www.devze.com 2023-03-19 22:52 出处:网络
I am using this regular expression to remove white space and line breaks from a 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

精彩评论

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