Blog > detail

Just my tiny mind

Code Compressor Ver 2

26-2-2012 Admin General 2 Comments

Code Compressor Ver 2

This code is the continuation of my previous Code Compressor. This Code Compressor Ver 2 can delete comments in php, html, javascript and css. However, this Code Compressor Ver 2 avoid to delete text that reside between quotes or double quotes.


Same with my previous Code Compressor Code Compressor Ver 2 also have 2 types of code. First is the HTML code to handle the form.


HTML code:


Code Compressor by Muhammad Hasan Septiadi


Then, the second one is the PHP code that process the inputted value from HTML code.


PHP code:


/*This code made by Muhammad Hasan Septiadi*/

if(isset($_POST['source']))

{

$str = $_POST['source'];

preg_match_all('/(?

foreach($found[0] as $key => $val){$str = str_replace($val, '[mhseptiadi*['.$key.']*mhseptiadi]', $str);}//strip the text between quotes

$str = preg_replace('@//(.*?)\n@i','',$str);//replace the comment for PHP and javascript that start with //

$str = preg_replace('@/\*(.*?)\*/@i','',$str);//replace the comment for PHP and javascript that start with /* and end with */

$str = preg_replace('@\<\!--(.*?)--\>@i','',$str);//replace the comment for HTML that start with

foreach($found[0] as $key => $val){$str = str_replace( '[mhseptiadi*['.$key.']*mhseptiadi]',$val, $str);}//the text between quotes back


echo "

Result";

echo htmlspecialchars(trim($str));

echo "

";

}


You can combine both of code into one file, that should be like:


Code Compressor by Muhammad Hasan Septiadi

/*This code made by Muhammad Hasan Septiadi*/

if(isset($_POST['source']))

{

$str = $_POST['source'];

preg_match_all('/(?

foreach($found[0] as $key => $val){$str = str_replace($val, '[mhseptiadi*['.$key.']*mhseptiadi]', $str);}//strip the text between quotes

$str = preg_replace('@//(.*?)\n@i','',$str);//replace the comment for PHP and javascript that start with //

$str = preg_replace('@/\*(.*?)\*/@i','',$str);//replace the comment for PHP and javascript that start with /* and end with */

$str = preg_replace('@\<\!--(.*?)--\>@i','',$str);//replace the comment for HTML that start with

foreach($found[0] as $key => $val){$str = str_replace( '[mhseptiadi*['.$key.']*mhseptiadi]',$val, $str);}//the text between quotes back


echo "

Result";

echo htmlspecialchars(trim($str));

echo "

";

}

?>


If we use the Code Compressor Ver 2 for our code, it should be:


Code Compressor by Muhammad Hasan Septiadi

$val){$str = str_replace($val, '[mhseptiadi*['.$key.']*mhseptiadi]', $str);} $str = preg_replace('@//(.*?)\n@i','',$str); $str = preg_replace('@/\*(.*?)\*/@i','',$str); $str = preg_replace('@\<\!--(.*?)--\>@i','',$str); foreach($found[0] as $key => $val){$str = str_replace( '[mhseptiadi*['.$key.']*mhseptiadi]',$val, $str);} echo "
Result"; echo htmlspecialchars(trim($str)); echo "
"; } ?>

Little explanation:

preg_match_all('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)"(.*\n.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'|(?<!\\\)\'(.*\n.*?)(?<!\\\)\'/i',   $str, $found);


The above code is used to capture all text between either sigle quotes (') or double quotes ("). However it will consider \' and \" as the text. The code will also capture text that have newline character if it is reside between either sigle quotes (') or double quotes (").


foreach($found[0] as $key => $val){$str = str_replace($val, '[mhseptiadi*['.$key.']*mhseptiadi]', $str);}


The above code will replace the text between quotes with something like [mhseptiadi*['.$key.']*mhseptiadi].


$str = preg_replace('@//(.*?)\n@i','',$str);

$str = preg_replace('@/\*(.*?)\*/@i','',$str);

$str = preg_replace('@\<\!--(.*?)--\>@i','',$str);


The above code is the code for removing the comment in php, javascript, HTML and css. You can add your own script to remove comment in other programming language.


foreach($found[0] as $key => $val){$str = str_replace( '[mhseptiadi*['.$key.']*mhseptiadi]',$val, $str);}


The above code will replace [mhseptiadi*['.$key.']*mhseptiadi] with the text between quotes.

If you found any bug in my code, fell free to tell me.

Good luck.


Related Posts:


Code Compressor

Code Compressor Ver 2 Code Compressor Ver 2 Code Compressor Ver 2

Code Compressor Ver 2 Code Compressor Ver 2 Code Compressor Ver 2

Code Compressor Ver 2

word word

Code Compressor Ver 2

Code Compressor Ver 2

Code Compressor Ver 2