Remove PHP comment and remove space function share _php tutorial

Source: Internet
Author: User
Tags php source code
Although PHP5 already have the Php_strip_whitespace method can return to delete comments and spaces after the PHP source function, in order to learn, here to provide you with a method, you can also remove the code blank and comments, the code is as follows:

Copy CodeThe code is as follows:
/**
* Remove whitespace and comments from the code
* @param string $content code content
* @return String
*/
function Strip_whitespace ($content) {
$STRIPSTR = ";
Analysis of 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;
}

http://www.bkjia.com/PHPjc/741259.html www.bkjia.com true http://www.bkjia.com/PHPjc/741259.html techarticle Although PHP5 already have the Php_strip_whitespace method can return to delete comments and spaces after the PHP source function, in order to learn, here to provide you with a way, you can also remove the generation ...

  • 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.