Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : Miscellaneous Functions : php_strip_whitespace


Return source with stripped comments and whitespace (PHP 5)
string php_strip_whitespace ( string filename )

Example 1361. php_strip_whitespace() example

// PHP comment here

* Another PHP comment

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

The above example will output:

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

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

Code Examples / Notes » php_strip_whitespace


With newlines stripped your HEREDOCs won't work.


This function can not be used to strip comments outside <?php ... ?>
// this comment will not be removed
// this comment will be removed


Notice: In my last comment for this function I failed to add some important parts of the function. So I have re-added it here. Feel free to delete my earlier comment. Thanks!
To use php_strip_whitespace in (PHP 4 >= 4.2.0) you could try the function below. This function also helps solve the issues with php_strip_whitespace not fully removing new lines and extra whitespace's in HTML when embedded with PHP.
if (!defined ('T_ML_COMMENT'))
if (!defined ('T_DOC_COMMENT'))
   define ('T_DOC_COMMENT', T_ML_COMMENT);
function StripWhitespace($sFileName)
   if ( !is_file($sFileName) )
       return false;
   $sContent = implode('', file($sFileName));
   $aTokens = token_get_all($sContent);
   $bLast = false;
   $sStr = '';
   for ( $i = 0, $j = count($aTokens); $i < $j; $i++ )
       if ( is_string($aTokens[$i]) )
           $bLast = false;
           $sStr .= $aTokens[$i];
           switch ( $aTokens[$i][0] )
               case T_COMMENT:
               case T_ML_COMMENT:
               case T_DOC_COMMENT:
               case T_WHITESPACE:
               if (!$bLast)
                   $sStr .= ' ';
                   $bLast = true;
                    $bLast = false;
                   $sStr .= $aTokens[$i][1];
   $sStr = trim($sStr);
   $sStr = str_replace("\n", "", $sStr);
   $sStr = str_replace("\r", "", $sStr);
   return $sStr;


Not only can this be used for JavaScript files, but also for:
* Java source code
* CSS (Style Sheets)
* Any file with C-style comments.


If you wish to just remove excess whitespace from a string, see the example "Strip whitespace" in the preg_replace documentation (

zvjezdan patz

I was given a report that was separated by spaces and asked to make graphs from it.  I needed to turn the report data into a csv in memory so I could manipulate it further.  
First needed to see the report, then need to strip out the whitespace, but leave one space between each item that I could convert to a column.
There were lots of complicated ways to do this.  I stumbled on something simple.
Say the report looks like this:
Monday    Tuesday    Wednesday    Thursday   Friday   Saturday   Sunday
1               5               7                    8               10         7              8        
7               15             4                    0               21         4              12
9               5               7                    9               0           9              43
The report is using spaces and not tabs to separate everything.  Assume it's a file called data.txt you can use the following to strip out the spaces and make it comma delimited:
$handle = @fopen("data.txt", "r");
if ($handle)
 while (!feof($handle))
   $buffer = fgets($handle, 4096);
   // this will search for 5 spaces and replace with 1, then 4, then 3, then 2
   // then only one will be left.  Replace that one space with a comma
   // then output with nl2br so you can see the line breaks
   print nl2br(str_replace(" ", ",",ereg_replace( '  ', ' ',ereg_replace( '   ', ' ', ereg_replace( '    ', ' ', ereg_replace( '     ',' ',$buffer ))))));
Hope that helps someone else.


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.
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=preg_replace('/\\n/', "%0A", $safe);
$safe=preg_replace('/\\t/', "%09", $safe);
$safe=preg_replace('/\\s/', "&nbsp;", $safe);
return $safe;
function unharden_characters($array)
$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:-
// ob_start(); and output here
header('Content-Encoding: gzip');
echo $data;


Here's one for CSS:
function css_strip_whitespace($css)
 $replace = array(
   "#/\*.*?\*/#s" => "",  // Strip C style comments.
   "#\s\s+#"      => " ", // Strip excess whitespace.
 $search = array_keys($replace);
 $css = preg_replace($search, $replace, $css);
 $replace = array(
   ": "  => ":",
   "; "  => ";",
   " {"  => "{",
   " }"  => "}",
   ", "  => ",",
   "{ "  => "{",
   ";}"  => "}", // Strip optional semicolons.
   ",\n" => ",", // Don't wrap multiple selectors.
   "\n}" => "}", // Don't wrap closing braces.
   "} "  => "}\n", // Put each rule on it's own line.
 $search = array_keys($replace);
 $css = str_replace($search, $replace, $css);
 return trim($css);
A word on the first regular expression, since it took me a while.
It strips C style comments. /* Like this. */
^         ^
The pound signs at either end quote the regex. They don't match anything.
The s at the very end sets the PCRE_DOTALL modifier. More info here:
#  /\*  .*?  \*/  #s
   1    2    3
The expression itself consists of 3 parts:
1. the opening comment sequence, represented by     /\*
2. everything in the middle, represented by         .*?
3. and the closing comment sequence, represented by \*/
  ^    ^
The comment asterisks are escaped. If I had used the more common / for PCRE quoting I would've had to escape those too.
The ? prevents the regex from being greedy. See halfway down this page:


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 !


@Zvjezdan Patz
Your script is quite the strain on the server. You might just want to do this (just the print line):
print preg_replace("/[ ]{1,}/", ",", trim($str));

Change Language

Follow Navioo On Twitter
eXTReMe Tracker