php_strip_whitespace

Opis

stringphp_strip_whitespace
( string$filename
)

Returns the PHP source code in filename with
PHP comments and whitespace removed. This may be useful for determining the
amount of actual code in your scripts compared with the amount of comments.
This is similar to using php -w from the
commandline.

Parametry

filename

Path to the PHP file.

Zwracane wartości

The stripped source code will be returned on success, or an empty string
on failure.

Informacja:

This function works as described as of PHP 5.0.1. Before this it would
only return an empty string. For more information on this bug and its
prior behavior, see bug report
» #29606.

Przykłady

Przykład #1 php_strip_whitespace() example

<?php// PHP comment here

/* * Another PHP comment */

echo php_strip_whitespace(__FILE__);// Newlines are considered whitespace, and are removed too:do_nothing();?>

Powyższy przykład wyświetli:

<?php
echo php_strip_whitespace(__FILE__); do_nothing(); ?>

Notice the PHP comments are gone, as are the whitespace and newline
after the first echo statement.

If you give a 'stream wrapped' path as argument, anything echoed by the stream wrapper during this call (e.g. trace messages) won't be displayed to the screen but will be inserted in php_strip_whitespace's result.

If you execute this stripped code later, it will display the messages which should have been output during php_strip_whitespace's execution !

I was looking earlier for a way to strip php comments from my source files but didn't come up with much. I wrote the following function to do the trick using the tokenizer. I've tested in on an entire phpMyAdmin install and it worked fine afterward... so it should be good to go. You may also specify any number of tokens to strip such as T_WHITESPACE rather the default of T_COMMENT and T_DOC_COMMENT.