Remove php comments and remove spaces function sharing_PHP tutorial

WBOY
Release: 2016-07-13 10:35:59
Original
796 people have browsed it

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

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!