How to remove php comments and space function sharing
This article introduces the knowledge of "how to remove php comments and remove space function sharing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Although there are php_strip_whitespace methods in php5 that can return the function of PHP source code after deleting comments and spaces, in order to learn, here we provide a method of your own, and you can also remove white space and comments in the code.
The copy code is as follows:
/ * *
* remove white space and comments from the code
* @ param string $content code content
* @ return string
, /
Function strip_whitespace ($content) {
$stripStr =''
/ / analyze the php source code
$tokens = token_get_all ($content)
$last_space = false
For ($I = 0, $j = count ($tokens); $I < $j; $iTunes +) {
If (is_string ($tokens [$I])) {
$last_space = false
$stripStr. = $tokens [$I]
} else {
Switch ($tokens [$I] [0]) {
/ / filter various PHP comments
Case T_COMMENT:
Case T_DOC_COMMENT:
Break
/ / filter spaces
Case T_WHITESPACE:
If (! $last_space) {
$stripStr. ='
$last_space = true
}
Break
Case T_START_HEREDOC:
$stripStr. = "