Remove all the line breaks from the html source

前端 未结 9 2045
情书的邮戳
情书的邮戳 2020-11-27 04:50

Well I know obfuscation is a bad idea. But I want all of my html code to come in one long single line. All the html tags are generated through PHP, so I think its possible.

9条回答
  •  忘掉有多难
    2020-11-27 05:23

    This is already well answered, but you may be able to do more than just trim spaces at both ends of each line:

    1. First extract all text within quotes (you don't want to touch those), replace with a marker with a sequence number, store the sequence number with the text
    2. Extract all text within tags and do the same as step #1
    3. Replace all white-space (including \n, \r) with spaces
    4. Replace all >1 space sequences with 1 space
    5. Replace all >_< with >< (_ = space)
    6. Replace all _>, <_ and with >, < and (_ = space)
    7. Replace markers with the actual texts

    This procedure can potentially compact the entire HTML file. This takes advantage of the fact that multiple white-space text inside HTML tags are intepreted as one single space.

提交回复
热议问题