retesere20

php-minify-old

Nov 27th, 2019
177
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://puvox.software/tools/php-minify
  2.  
  3. CODE:
  4.  
  5.  
  6. <?php
  7. $use_builtin = true; //true( based on: https://www.php.net/manual/en/function.php-strip-whitespace.php ) or false ( based on: https://stackoverflow.com/q/503871/2377343 )
  8.  
  9. $source_content ='';
  10. $final_content='';
  11. if(isset($_POST['phpminify_code'])){
  12. $source_content = $_POST['phpminify_code'];
  13. $new_content = $source_content;
  14. //auto append <?php
  15. if (stripos($new_content, $pre='<?php')===false) {
  16. $auto_added=true;
  17. $new_content = $pre." ".$new_content;
  18. }
  19. //=================
  20.  
  21. if ($use_builtin){
  22. $file= __DIR__.'/temp_1'.rand(1,99999999).rand(1,99999999);
  23. file_put_contents($file, $new_content);
  24. $final_content=php_strip_whitespace($file);
  25. $final_content= str_replace(" ", " ", $final_content);
  26. $final_content= str_replace(["= "," ="], "=", $final_content);
  27. $final_content= str_replace(["; "], ";", $final_content);
  28. $final_content= str_replace([", "], ",", $final_content);
  29. $final_content= str_replace([". "], ".", $final_content);
  30. $final_content= str_replace(["{ "], "{", $final_content);
  31. $final_content= str_replace(["} "], "}", $final_content);
  32. $final_content= str_replace(["[ "], "[", $final_content);
  33. $final_content= str_replace(["] "], "]", $final_content);
  34. // ...
  35. // ...
  36. // ...
  37. // ...
  38. // I've found out here, that this approach is not ideal.
  39. unlink($file);
  40. }
  41. else{
  42. $final_content = '';
  43. $commentTokens = array(T_COMMENT, T_DOC_COMMENT);
  44. foreach ( token_get_all($content) as $token) {
  45. if (is_array($token)) {
  46. if (in_array($token[0], $commentTokens))
  47. continue;
  48. $token = $token[1];
  49. }
  50. $final_content .= $token;
  51. }
  52. }
  53. if (isset($auto_added)){
  54. $final_content=str_replace($pre,'', $final_content);
  55. }
  56. }
  57. ?>
  58. <style>
  59. .eachBlock{
  60. width: 50%;
  61. margin: 0 0 0 25%;
  62. }
  63. textarea{
  64. width:100%; resize: both;
  65. height: 350px;
  66. }
  67. .submit_parent{ text-align: center; }
  68. .centered { justify-content: center; text-align:center; }
  69.  
  70. .row{ margin: 30px 0; flex-direction: column; }
  71.  
  72. .titlerow{ margin:0 0 20px 0 ; }
  73. </style>
  74.  
  75. <div class="row maxwidth centered titlerow">
  76. <h1 style="color:#0070ff;">PHP Minify</h1>
  77. <div class="ui-state-highlight">Experimental & poor parser to removes comments, white-spaces and new-lines from PHP source-code.</div>
  78. </div>
  79.  
  80.  
  81. <div class="row maxwidth source_row">
  82. <div class="eachBlock">
  83. <div class="formDiv">
  84. <h2>Enter source code:</h2>
  85. <p>Note, script auto-assigns <code>&lt;?php</code> in the start</p>
  86. <form action="" method="post" enctype="multipart/form-data">
  87. <textarea name="phpminify_code"><?php echo htmlentities($source_content);?></textarea>
  88. <div class="submit_parent">
  89. <input type="submit" id="submitbtn" value="Minify">
  90. </div>
  91. <input type="hidden" name="update" value="ok">
  92. </form>
  93. <script>
  94. $(function(){
  95. $('#submitbtn').button();
  96. //$('input:text, input:password, input[type=email], button').button().addClass('ui-textfield');
  97. $('input:text, input:password, input[type=email]').addClass('ui-textfield ui-corner-all');
  98. });
  99. </script>
  100. </div>
  101. </div>
  102. </div>
  103.  
  104.  
  105.  
  106. <div class="row maxwidth results_row">
  107. <div class="eachBlock">
  108. <h2 class="centered" style="color:red;">Result:</h2>
  109. <textarea><?php echo htmlentities($final_content);?></textarea>
  110. </div>
  111. </div>
RAW Paste Data