note 77611 deleted from function.php-strip-whitespace by nlopess
| From: | nlopess@php.net | Date: | Thu, 06 Sep 2007 13:31:34 +0000 |
| Subject: | note 77611 deleted from function.php-strip-whitespace by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-130248@lists.php.net to get a copy of this message | ||
Note Submitter: natio at phpfox dot com
----
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.
<?php
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];
}
else
{
switch ( $aTokens[$i][0] )
{
case T_COMMENT:
case T_ML_COMMENT:
case T_DOC_COMMENT:
break;
case T_WHITESPACE:
if (!$bLast)
{
$sStr .= ' ';
$bLast = true;
}
break;
default:
$bLast = false;
$sStr .= $aTokens[$i][1];
break;
}
}
}
$sStr = trim($sStr);
$sStr = str_replace("\n", "", $sStr);
$sStr = str_replace("\r", "", $sStr);
return $sStr;
}
?>