Code Compressor Ver 2

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:

<h1>Code Compressor by Muhammad Hasan Septiadi</h1>
<!--This code made by Muhammad Hasan Septiadi-->
<form action="" method="post" align="center">
<textarea name="source" style="width:100%;height:300px;" id="source"></textarea>
<input type="submit" value="Minimize"/>
</form>

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('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)"(.*\n.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'|(?<!\\\)\'(.*\n.*?)(?<!\\\)\'/i',   $str, $found);//to capture text between " or '

	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 <!-- and end with -->
	foreach($found[0] as $key => $val){$str = str_replace( '[mhseptiadi*['.$key.']*mhseptiadi]',$val, $str);}//the text between quotes back

	echo "<fieldset><legend>Result</legend>";
	echo htmlspecialchars(trim($str));
	echo "</fieldset>";
}

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

<h1>Code Compressor by Muhammad Hasan Septiadi</h1>
<!--This code made by Muhammad Hasan Septiadi-->
<form action="" method="post" align="center">
<textarea name="source" style="width:100%;height:300px;" id="source"></textarea>
<input type="submit" value="Minimize"/>
</form>
<?php
/*This code made by Muhammad Hasan Septiadi*/
if(isset($_POST['source']))
{
	$str = $_POST['source'];
	preg_match_all('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)"(.*\n.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'|(?<!\\\)\'(.*\n.*?)(?<!\\\)\'/i',   $str, $found);//to capture text between " or '

	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 <!-- and end with -->
	foreach($found[0] as $key => $val){$str = str_replace( '[mhseptiadi*['.$key.']*mhseptiadi]',$val, $str);}//the text between quotes back

	echo "<fieldset><legend>Result</legend>";
	echo htmlspecialchars(trim($str));
	echo "</fieldset>";
}
?>

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

<h1>Code Compressor by Muhammad Hasan Septiadi</h1> <form action="" method="post" align="center"> <textarea name="source" style="width:100%;height:300px;" id="source"></textarea> <input type="submit" value="Minimize"/> </form> <?php if(isset($_POST['source'])) { $str = $_POST['source']; preg_match_all('/(?<!\\\)"(.*?)(?<!\\\)"|(?<!\\\)"(.*\n.*?)(?<!\\\)"|(?<!\\\)\'(.*?)(?<!\\\)\'|(?<!\\\)\'(.*\n.*?)(?<!\\\)\'/i', $str, $found);	 foreach($found[0] as $key => $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 "<fieldset><legend>Result</legend>"; echo htmlspecialchars(trim($str)); echo "</fieldset>"; } ?>

Little explanation:

preg_match_all('/(?&lt;!\\\)"(.*?)(?&lt;!\\\)"|(?&lt;!\\\)"(.*\n.*?)(?&lt;!\\\)"|(?&lt;!\\\)\'(.*?)(?&lt;!\\\)\'|(?&lt;!\\\)\'(.*\n.*?)(?&lt;!\\\)\'/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

http://septiadi.com/2011/03/08/code-compressor-ver-2/

 

Advertisements

One response to “Code Compressor Ver 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: