[HOW-TO] Ultimate search and destroy (feat. sed, php file, multline, slash, and asterix)

Hi,

I want to do a multiline search and delete with sed, deleting strings starting with /* and endding with *\ (removing multiline comments). After that I will pipe that into other seds to remove inline comments, than I will remove all new lines, then than replace all occurences of “<?php” with "<?php " (put spaces after <?php), and than delete double spaces. However, I can’t figure out the step 1. Anyone know how to do it.

Note: I am trying to get 1 long line php file, if any easier way to do this, I would appreciate your guideness.

Thanks in advance.

Yaşar Arabacı.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I’m sure there’s an interesting explanation behind this exercise; care to
share?

Can it be done? Sure, I’m sure it can, but is it worth the risk of
introducing bugs in code needlessly? Doubt it.

Good luck.

On 12/17/2010 09:06 PM, yasar11732 wrote:
>
> Hi,
>
> I want to do a multiline search and delete with sed, deleting strings
> starting with /* and endding with *\ (removing multiline comments).
> After that I will pipe that into other seds to remove inline comments,
> than I will remove all new lines, then than replace all occurences of
> “<?php” with "<?php " (put spaces after <?php), and than delete double
> spaces. However, I can’t figure out the step 1. Anyone know how to do
> it.
>
> Note: I am trying to get 1 long line php file, if any easier way to do
> this, I would appreciate your guideness.
>
> Thanks in advance.
>
> Yaşar Arabacı.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJNDDvYAAoJEF+XTK08PnB5sJcQAIr4xTFGtwZx15Y1t+P4I1g9
sDPv5kYkzP8va4CfiqsJATLbdxCHyK+hmVVKU60t2c0Gvt3y7zsb4kGjCOF+mecC
w0UmyKtXw23TNlcPz3rBgySx1xefbr6jnj1pGKt8rhnqV/WlZFq+3wLDpN8Ke2yC
Y17dV69i14WhdbTMet5hh9jSJ8KvQIq5P2B8Os2nT4NzTvrlA/UjIO12ldKjt+E7
fdYdTt3PV9KvvXvvfUXpR3cdpfi/0lSNYbdAgCdtZl9z1JeYSONzR+irNj/MH1uL
8QP99QVBlmmNKRUasf6Un/bbtfcTSXj1ZKKgCbpaw4m9q5VYVNtW7CETKE8lpUC9
CpYHJ3qAOIXOUgYU6Swqd9dYCfmuSQdrhFK/PeFFWQOVIrBGSa79WqYU9nD6i9jX
doJdpSmlJ9rwer6NLqLjZZsU2tzTcGA+UvP9qZTM82g5xbiKPt/vNTGZsTvJc+WH
p0eue192VQJJOmFk/2tQvOiCXJJDDH9S/x258yQWIHS59oEXxzIT2y9PLBApGeB8
P6bjpssWMzFKYn3w2Q5ppRhZTL2bPZUmrOxXCFyGfnVhdT3RCXlBRAR+FjJVqlsr
SRwVQtVCaEgyM/r0jpcGLrV0pnn/Djgdk834AyWKSQT5swAi52etU7fouGXVuUlh
LWoSE+NgXZNr3rOqDX2K
=LB38
-----END PGP SIGNATURE-----

Well, I am doing this just for fun really, what I am to do after completing this step is, I ll create a for loop that,recursively minimalizing all php files in a given directory (I managed to get working copy of original file with one third space consuming for now), and instead of saving the output files to my computer, it will save output to remote server via ftp protocol, so that while keeping my human readible source files, my web server will contain only less space consuming files.

Edit: It is can be seen as kind of compressed file transfer of php files :slight_smile:

Look at php_strip_whitespace in the PHP core functions, or php -w from the command line.

Wow, this is easier than I thought :slight_smile: thx …

I have used:


for d in $(find -name \*.php);do
> cp $d $d.backup
> php -w $d.backup > $d
> rm $d.backup
> done

This code to remove comments and other unnecessary whitespaces. It seems like it worked so far. But if you want to use this code, use at your own risk. I don’t guarante that it would work as intended. I recommend backing for files before running this, and change your working directory to where you want to change php, keep in mind that his wil change files recursively

This doesn’t only remove comments but also strips whitespaces (new lines). If you just want to remove comments while leaving the scripts human readable, you should rather use the PHP tokenizer like in the function on that page: PHP: php_strip_whitespace - Manual