Php clear line breaks, clear tabs, remove comment mark Summary-PHP source code

Source: Internet
Author: User
This article summarizes several methods of using php to clear line breaks, tabs, and comment mark to implement code. If you need it, refer to this article. This article summarizes several methods of using php to clear line breaks, tabs, and comment mark to implement code. If you need it, refer to this article.

Script ec (2); script

The Code is as follows:

/**
* Html compression: clear line breaks, clear tabs, and remove comment tags
* @ Param $ string
* @ Return $ string after compression
**/
Function compress_html ($ string ){
$ String = str_replace ("rn", '', $ string); // clear the linefeed.
$ String = str_replace ("n", '', $ string); // clear the linefeed.
$ String = str_replace ("t", '', $ string); // clear the tab
$ Pattern = array (
"/> * ([^] *) * "/[S] + /",
"/ /",
"/"/",
"/"/",
"'/* [^ *] **/'"
);
$ Replace = array (
"> \ 1 <",
"",
"",
""",
""",
""
);
Return preg_replace ($ pattern, $ replace, $ string );
}

Remove consecutive spaces and line breaks

The Code is as follows:

$ Str = "I am a booknnnnnmoth ";
// Remove all spaces and line breaks
Echo preg_replace ("/[s] {2,}/", "", $ str ).'
';
// Remove unnecessary spaces and line breaks and retain only one
Echo preg_replace ("/([s] {2,})/", "\ 1", $ str );
?>

Remove line breaks

Preg_replace ("'([rn]) [s] +'", "", $ content) // remove the carriage return line break

The Code is as follows:

// $ Document should contain an HTML document.
// In this example, the HTML Tag and javascript code are removed.
// And blank characters. Some common
// Convert the HTML object to the corresponding text.

$ Search = array ("' ] *?>. *? Script 'si ", // remove javascript
"'<[/!] *? [^ <>] *?> 'Si ", // remove the HTML Tag
"'([Rn]) [s] +'", // remove the white space
"'& (Quot | #34);' I", // replaces the HTML Object
"'& (Amp | #38);' I ",
"'& (Lt | #60);' I ",
"'& (Gt | #62);' I ",
"'& (Nbsp | #160);' I ",
"'& (Iexcl | #161);' I ",
"'& (Cent | #162);' I ",
"'& (Pound | #163);' I ",
"'& (Copy | #169);' I ",
"'& # (D +); 'E"); // run as PHP code

$ Replace = array ("",
"",
"\ 1 ",
""",
"&",
"<",
"> ",
"",
Chr (1, 161 ),
Chr (1, 162 ),
Chr (1, 163 ),
Chr (1, 169 ),
"Chr (\ 1 )");

$ Text = preg_replace ($ search, $ replace, $ document );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.