PHP file to remove PHP annotation space function Analysis (PHP code compression) _php Tips

Source: Internet
Author: User
Recently I from thinkphp ' Runtime_allinone ' learn from the experience: not afraid of the cache more, just afraid of the call chaos, simply will all the commonly used documents merged into a file, not beautiful ...
Copy Code code as follows:

function Strip_whitespace ($content) {
$stripStr = ';
Analyze 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 annotations
Case T_comment:
Case T_doc_comment:
Break
Filter spaces
Case T_whitespace:
if (! $last _space) {
$stripStr. = ';
$last _space = true;
}
Break
Default
$last _space = false;
$stripStr. = $tokens [$i][1];
}
}
}
return $stripStr;
}

The custom function effectively solves the problem of <<<eot (Heredoc) in the Php_strip_whitespace system, which cannot correctly understand the space function.
How to use
Copy Code code as follows:

$str = Strip_whitespace (' <?php '. $str);

The front must stitching this, I also do not understand, do not splice the result of the execution generated is the result of the error
Php_strip_whitespace
String Php_strip_whitespace (String$filename)
If it is only a single file and there is no heredoc, it is recommended to use the shortcut Php_strip_whitespaceFunction

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.