Remove php comments and remove spaces function sharing _ PHP Tutorial

Source: Internet
Author: User
Remove php comments and remove space function sharing. Although the php_strip_whitespace method already exists in php5, it can return the PHP source code function after deleting comments and spaces. to learn this, we provide you with a method of our own, it can also be removed. Although the php_strip_whitespace method already exists in php5, it can return the PHP source code function after deleting comments and spaces. to learn more, here we provide you with a method of our own, you can also remove spaces and comments from the code. the code is as follows:

The code is as follows:


/**
* Remove blank and comments from the code
* @ Param string $ content code content
* @ Return string
*/
Function strip_whitespace ($ content ){
$ StripStr = '';
// Analyze the php source code
$ Tokens = token_get_all ($ content );
$ Last_space = false;
For ($ I = 0, $ j = count ($ tokens); $ I <$ j; $ I ++ ){
If (is_string ($ tokens [$ I]) {
$ Last_space = false;
$ StripStr. = $ tokens [$ I];
} Else {
Switch ($ tokens [$ I] [0]) {
// Filter various PHP comments
Case T_COMMENT:
Case T_DOC_COMMENT:
Break;
// Filter spaces
Case T_WHITESPACE:
If (! $ Last_space ){
$ StripStr. = '';
$ Last_space = true;
}
Break;
Case T_START_HEREDOC:
$ StripStr. = "<
Break;
Case T_END_HEREDOC:
$ StripStr. = "THINK; \ n ";
For ($ k = $ I + 1; $ k <$ j; $ k ++ ){
If (is_string ($ tokens [$ k]) & $ tokens [$ k] = ';'){
$ I = $ k;
Break;
} Else if ($ tokens [$ k] [0] = T_CLOSE_TAG ){
Break;
}
}
Break;
Default:
$ Last_space = false;
$ StripStr. = $ tokens [$ I] [1];
}
}
}
Return $ stripStr;
}

...

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.