php_strip_whitespace

(PHP 5)

php_strip_whitespace --  Return source with stripped comments and whitespace

说明

string php_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.

注: 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.

返回值

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

范例

例子 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();
?>

上例将输出:

<?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.


add a note add a note User Contributed Notes
flconseil at yahoo dot fr
08-Jul-2006 11:57
Beware that this function uses the output buffering mechanism.

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 !
mwwaygoo AT hotmail DOT com
28-Apr-2006 12:17
I thought this was a nice function until I realised it wouldnt strip down html. As i'd been reading an article on compressing output to speed up delivery.
So I wrote a little one to do that for me. Here its is, incase people were looking for a html version. It may need tweaking, like with existing &nbsp;'s.

<?php
function strip_html($data)
{
  
// strip unecessary comments and characters from a webpages text
   // all line comments, multi-line comments \\r \\n \\t multi-spaces that make a script readable.
   // it also safeguards enquoted values and values within textareas, as these are required

  
$data=preg_replace_callback("/>[^<]*<\\/textarea/i", "harden_characters", $data);
  
$data=preg_replace_callback("/\\"[^"<>]+\\"/", "harden_characters", $data);

   $data=preg_replace("
/(//.*n)/","",$data); // remove single line comments, like this, from // to \\n
   $data=preg_replace("
/(t|r|n)/","",$data);  // remove new lines \\n, tabs and \\r
   $data=preg_replace("
/(/*.**/)/","",$data);  // remove multi-line comments /* */
   $data=preg_replace("
/(<![^>]*>)/","",$data);  // remove multi-line comments <!-- -->
   $data=preg_replace('/(\\s+)/', ' ',$data); // replace multi spaces with singles
   $data=preg_replace('/>\\s</', '><',$data);

   $data=preg_replace_callback("
/"[^\\"<>]+"/", "unharden_characters", $data);
  
$data=preg_replace_callback("/>[^<]*<\\/textarea/", "unharden_characters", $data);

   return
$data;
}

function
harden_characters($array)
{
  
$safe=$array[0];
  
$safe=preg_replace('/\\n/', "%0A", $safe);
  
$safe=preg_replace('/\\t/', "%09", $safe);
  
$safe=preg_replace('/\\s/', "&nbsp;", $safe);
   return
$safe;
}
function
unharden_characters($array)
{
  
$safe=$array[0];
  
$safe=preg_replace('/%0A/', "\\n", $safe);
  
$safe=preg_replace('/%09/', "\\t", $safe);
  
$safe=preg_replace('/&nbsp;/', " ", $safe);
   return
$safe;
}
?>

The article code was similar to this, which shouldn't work as php_strip_whitespace takes a filename as input:-

<?php
// ob_start(); and output here
$data=ob_get_contents();
ob_end_clean();
if(
strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip'))
{
  
$data=gzencode(php_strip_whitespace($data),9);
  
header('Content-Encoding: gzip');
}
echo
$data;
?>
Cosmo
01-Apr-2006 07:27
With newlines stripped your HEREDOCs won't work.
dnc at seznam dot cz
22-Oct-2005 06:01
This function can not be used to strip comments outside <?php ... ?>

// this comment will not be removed
<?php
// this comment will be removed
?>
amedee at amedee dot be
09-Jul-2005 08:29
Not only can this be used for JavaScript files, but also for:

* Java source code
* CSS (Style Sheets)
* Any file with C-style comments.