search
Homephp教程php手册去除PHP代码中的空白和注释


PHP内置了一个php_strip_whitespace方法用于读取php文件并去除代码中的空白和注释,但不支持直接读取内容去除空白和注释,下面的方法则可以支持读取字符串内容,并且ThinkPHP框架内置了该方法。/**<br>  * 去除代码中的空白和注释<br>  * @param string $content 代码内容<br>  * @return string<br>  */<br> function strip_whitespace($content) {<br>     $stripStr   = '';<br>     //分析php源码<br>     $tokens     = token_get_all($content);<br>     $last_space = false;<br>     for ($i = 0, $j = count($tokens); $i          if (is_string($tokens[$i])) {<br>             $last_space = false;<br>             $stripStr  .= $tokens[$i];<br>         } else {<br>             switch ($tokens[$i][0]) {<br>                 //过滤各种PHP注释<br>                 case T_COMMENT:<br>                 case T_DOC_COMMENT:<br>                     break;<br>                 //过滤空格<br>                 case T_WHITESPACE:<br>                     if (!$last_space) {<br>                         $stripStr  .= ' ';<br>                         $last_space = true;<br>                     }<br>                     break;<br>                 case T_START_HEREDOC:<br>                     $stripStr .= "                     break;<br>                 case T_END_HEREDOC:<br>                     $stripStr .= "THINK;\n";<br>                     for($k = $i+1; $k                          if(is_string($tokens[$k]) && $tokens[$k] == ';') {<br>                             $i = $k;<br>                             break;<br>                         } else if($tokens[$k][0] == T_CLOSE_TAG) {<br>                             break;<br>                         }<br>                     }<br>                     break;<br>                 default:<br>                     $last_space = false;<br>                     $stripStr  .= $tokens[$i][1];<br>             }<br>         }<br>     }<br>     return $stripStr;<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software