Remove PHP comments and whitespace functions

Source: Internet
Author: User
Tags php source code
  1. /**
  2. * Remove whitespace and comments from the code
  3. * @param string $content code content
  4. * @return String
  5. */
  6. function Strip_whitespace ($content) {
  7. $STRIPSTR = ";
  8. Analysis of PHP source code
  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. Filter various PHP comments
  18. Case T_comment:
  19. Case T_doc_comment:
  20. Break
  21. Filter spaces
  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. }
Copy Code
  • 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.