Although there is a php_strip_whitespace method in php5 that can return the PHP source code after removing comments and spaces. For the sake of learning, here is a method of your own that can also remove whitespace and comments in the code. The code is as follows:
Copy code The code is as follows:
/**
* Remove blanks and comments in the code
* @param string $content code content
* @return string
*/
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; {
//Filter various PHP comments
case T_COMMENT:
case T_DOC_COMMENT:
break;
//Filter spaces
case T_WHITESPACE:
if (!$last_space) {
$stripStr break;
case T_START_HEREDOC:
$stripStr .= "<< break;
case T_END_HEREDOC:
$stripStr .= "THINK;n";
for($k = $i+1; $k < $j; $k++) { [$k][0] == T_CLOSE_TAG) {
break;
default:
$last_space = false;
$stripStr .= $tokens[$i][1];
return $stripStr;
}
http://www.bkjia.com/PHPjc/741259.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/741259.htmlTechArticleAlthough there is a php_strip_whitespace method in php5 that can return the PHP source code after removing comments and spaces. In order to learn, here Provide you with your own method, you can also remove the code...