How replace all spaces inside HTML elements with using preg_replace?
问题 I need replace spaces with inside HTML elements. Example: <table atrr="zxzx"><tr> <td>adfa a adfadfaf></td><td><br /> dfa dfa</td> </tr></table> should become <table atrr="zxzx"><tr> <td>adfa a adfadfaf></td><td><br /> dfa dfa</td> </tr></table> 回答1: use regex to catch data between tags (?:<\/?\w+)(?:\s+\w+(?:\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+)?)+\s*|\s*)\/?>([^<]*)? then replace ' ' with ' ' also to catch before and after html : ^([^<>]*)<? >([^<>]*)$ Edit: here you go.... <?php $data=