Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://puvox.software/tools/php-minify
- CODE:
- <?php
- $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 )
- $source_content ='';
- $final_content='';
- if(isset($_POST['phpminify_code'])){
- $source_content = $_POST['phpminify_code'];
- $new_content = $source_content;
- //auto append <?php
- if (stripos($new_content, $pre='<?php')===false) {
- $auto_added=true;
- $new_content = $pre." ".$new_content;
- }
- //=================
- if ($use_builtin){
- $file= __DIR__.'/temp_1'.rand(1,99999999).rand(1,99999999);
- file_put_contents($file, $new_content);
- $final_content=php_strip_whitespace($file);
- $final_content= str_replace(" ", " ", $final_content);
- $final_content= str_replace(["= "," ="], "=", $final_content);
- $final_content= str_replace(["; "], ";", $final_content);
- $final_content= str_replace([", "], ",", $final_content);
- $final_content= str_replace([". "], ".", $final_content);
- $final_content= str_replace(["{ "], "{", $final_content);
- $final_content= str_replace(["} "], "}", $final_content);
- $final_content= str_replace(["[ "], "[", $final_content);
- $final_content= str_replace(["] "], "]", $final_content);
- // ...
- // ...
- // ...
- // ...
- // I've found out here, that this approach is not ideal.
- unlink($file);
- }
- else{
- $final_content = '';
- $commentTokens = array(T_COMMENT, T_DOC_COMMENT);
- foreach ( token_get_all($content) as $token) {
- if (is_array($token)) {
- if (in_array($token[0], $commentTokens))
- continue;
- $token = $token[1];
- }
- $final_content .= $token;
- }
- }
- if (isset($auto_added)){
- $final_content=str_replace($pre,'', $final_content);
- }
- }
- ?>
- <style>
- .eachBlock{
- width: 50%;
- margin: 0 0 0 25%;
- }
- textarea{
- width:100%; resize: both;
- height: 350px;
- }
- .submit_parent{ text-align: center; }
- .centered { justify-content: center; text-align:center; }
- .row{ margin: 30px 0; flex-direction: column; }
- .titlerow{ margin:0 0 20px 0 ; }
- </style>
- <div class="row maxwidth centered titlerow">
- <h1 style="color:#0070ff;">PHP Minify</h1>
- <div class="ui-state-highlight">Experimental & poor parser to removes comments, white-spaces and new-lines from PHP source-code.</div>
- </div>
- <div class="row maxwidth source_row">
- <div class="eachBlock">
- <div class="formDiv">
- <h2>Enter source code:</h2>
- <p>Note, script auto-assigns <code><?php</code> in the start</p>
- <form action="" method="post" enctype="multipart/form-data">
- <textarea name="phpminify_code"><?php echo htmlentities($source_content);?></textarea>
- <div class="submit_parent">
- <input type="submit" id="submitbtn" value="Minify">
- </div>
- <input type="hidden" name="update" value="ok">
- </form>
- <script>
- $(function(){
- $('#submitbtn').button();
- //$('input:text, input:password, input[type=email], button').button().addClass('ui-textfield');
- $('input:text, input:password, input[type=email]').addClass('ui-textfield ui-corner-all');
- });
- </script>
- </div>
- </div>
- </div>
- <div class="row maxwidth results_row">
- <div class="eachBlock">
- <h2 class="centered" style="color:red;">Result:</h2>
- <textarea><?php echo htmlentities($final_content);?></textarea>
- </div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement