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);
精彩评论