Remove php comments and spaces function

WBOY
Release: 2016-07-25 08:53:54
Original
653 people have browsed it
  1. /**
  2. * Remove blanks and comments in the code
  3. * @param string $content code content
  4. * @return string
  5. */
  6. function strip_whitespace($content) {
  7. $stripStr = '';
  8. //分析php源码
  9. $tokens = token_get_all($content);
  10. $last_space = false;
  11. for ($i = 0, $j = count($tokens); $i < $j; $i++) {
  12. if (is_string($tokens[$i])) {
  13. $last_space = false;
  14. $stripStr .= $tokens[$i];
  15. } else {
  16. switch ($tokens[$i][0]) {
  17. //过滤各种PHP注释
  18. case T_COMMENT:
  19. case T_DOC_COMMENT:
  20. break;
  21. //过滤空格
  22. case T_WHITESPACE:
  23. if (!$last_space) {
  24. $stripStr .= ' ';
  25. $last_space = true;
  26. }
  27. break;
  28. case T_START_HEREDOC:
  29. $stripStr .= "<<
  30. break;
  31. case T_END_HEREDOC:
  32. $stripStr .= "THINK;n";
  33. for($k = $i+1; $k < $j; $k++) {
  34. if(is_string($tokens[$k]) && $tokens[$k] == ';') {
  35. $i = $k;
  36. break;
  37. } else if($tokens[$k][0] == T_CLOSE_TAG) {
  38. break;
  39. }
  40. }
  41. break;
  42. default:
  43. $last_space = false;
  44. $stripStr .= $tokens[$i][1];
  45. }
  46. }
  47. }
  48. return $stripStr;
  49. }
复制代码


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!