Remove PHP annotations and remove space function sharing

Source: Internet
Author: User

  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; }  

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.