note 63922 deleted from function.php-strip-whitespace by bjori
| From: | bjori@php.net | Date: | Tue, 04 Apr 2006 09:22:01 +0000 |
| Subject: | note 63922 deleted from function.php-strip-whitespace by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-107272@lists.php.net to get a copy of this message | ||
Note Submitter: heavyraptor
Reason: useless
----
My previous post was ok, but there where some problems; that's why I got this code, which is
identical to the original function of PHP, because its the same code (not exactly beacuse I changed
some little things to make it easier).
Enjoy
<?php
// Replaces the Function for users with PHP-Version < 5.0.1
if (!function_exists('php_strip_whitespace')) {
function php_strip_whitespace($file)
{
if (!is_scalar($file)) {
user_error('php_strip_whitespace() expects parameter 1 to be string, ' .
gettype($file) . ' given', E_USER_WARNING);
return;
}
$source = file_get_contents($file);
$tokens = token_get_all($source);
$source = '';
$was_ws = false;
foreach ($tokens as $token) {
if (is_string($token)) {
$source .= $token;
} else {
list($id, $text) = $token;
switch ($id) {
case T_COMMENT:
case T_ML_COMMENT:
case T_DOC_COMMENT:
break;
case T_WHITESPACE:
if ($was_ws !== true) {
$source .= ' ';
}
$was_ws = true;
break;
default:
$was_ws = false;
$source .= $text;
break;
}
}
}
return $source;
}
}
?>