Код: Выделить всё
class IgnoreWhiteSpaces
{
function process($s)
{
// Temporary buffer
$GLOBALS['__IgnoreWhiteSpaces__process__'] = array();
// Save all problem parts
$s = preg_replace_callback(
"/(<(script|style)[^>]*?>((?!<\/\\2>).)*?<\/\\2>)/s",
array('IgnoreWhiteSpaces', '_ob_parse'),
$s
);
// Strip all white spaces
$s = preg_replace('/\s+/', '', $s);
// Restore all saved parts
$s = preg_replace(
array_values($GLOBALS['__IgnoreWhiteSpaces__process__']),
array_keys($GLOBALS['__IgnoreWhiteSpaces__process__']),
$s
);
// Destroy buffer
unset($GLOBALS['__IgnoreWhiteSpaces__process__']);
// Return result
return $s;
}
function _ob_parse($matches)
{
// Generate hash of string
$s = $matches[1];
$md5 = md5($s);
// Save this string
$GLOBALS['__IgnoreWhiteSpaces__process__'][$s] = '/' . $md5 . '/';
// Make here the more delicate handle of embed scripts
// Return hash of saved string
return $md5;
}
}
пользовать так
Код: Выделить всё
echo IgnoreWhiteSpaces::process($s);