This article mainly introduces the removal of PHP annotation and remove the space function, PHP5 has provided the same function, here only for learning PHP, need friends can refer to the following
Although there are already php_strip_whitespace methods in PHP5 to return the function of PHP source code after removing comments and spaces, in order to learn, here provides a way for you to get rid of the blanks and comments in the code as follows: code as follows: /** * removes whitespace and annotations * @param string $content code content * @return string */function Strip_whitespace ($con Tent) { $stripStr = '; //analysis PHP source $tokens = Token_get_all ($ content); $last _space = false; for ($i = 0, $j = count ($tokens) $i < $j $i + +) { is_string ($tokens [$i]) { $last _space = false; $STRIPSTR &NB SP;. = $tokens [$i]; } else { switch ($tokens [$i][0]) { &NB Sp //filter various PHP comments Case t_comment: &NB Sp case t_doc_comment: break; //Filter space CAS E t_whitespace: if (! $last _space) { &N Bsp $STRIPSTR .= '; $last _space = true; & nbsp break; case T_start_heredoc: &N Bsp $STRIPSTR. = "<<<thinkn"; break; case T_end_heredoc: &NBSP $stripStr. = "Think;n"; for ($k = $i +1; $k < $j; $k + +) { &NB Sp is_string ($tokens [$k]) && $tokens [$k] = = '; { $i = $k; break; } else if ($tokens [$k][0] = = T_close_ta G) { break; &NBSP ; & nbsp } &NBSP Break default: & nbsp $last _space = false; $STRIPSTR .= $tokens [$i][1]; { } return $stripStr; }