up.
Dai, possibile che non ci sia nessuno che sappia scrivere una regexp neanche troppo complicata?
Per adesso, a chi interessa, ho risolto con due funzioni "prese in prestito" da Smarty:
Codice PHP:
function trimwhitespace($source){
preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
$_script_blocks = $match[0];
$source = preg_replace("!<script[^>]+>.*?</script>!is",'@@@COMPRESSOR:TRIM:SCRIPT@@@', $source);
preg_match_all("!<pre>.*?</pre>!is", $source, $match);
$_pre_blocks = $match[0];
$source = preg_replace("!<pre>.*?</pre>!is",'@@@COMPRESSOR:TRIM:PRE@@@', $source);
preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match);
$_textarea_blocks = $match[0];
$source = preg_replace("!<textarea[^>]+>.*?</textarea>!is",'@@@COMPRESSOR:TRIM:TEXTAREA@@@', $source);
$source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
trimwhitespace_replace("@@@COMPRESSOR:TRIM:TEXTAREA@@@",$_textarea_blocks, $source);
trimwhitespace_replace("@@@COMPRESSOR:TRIM:PRE@@@",$_pre_blocks, $source);
trimwhitespace_replace("@@@COMPRESSOR:TRIM:SCRIPT@@@",$_script_blocks, $source);
return $source;
}
function trimwhitespace_replace($search_str, $replace, &$subject) {
$_len = strlen($search_str);
$_pos = 0;
for ($_i=0, $_count=count($replace); $_i<$_count; $_i++)
if (($_pos=strpos($subject, $search_str, $_pos))!==false)
$subject = substr_replace($subject, $replace[$_i], $_pos, $_len);
else
break;
}
si richiama così:
Codice PHP:
$html_pulito = trimwhitespace($html);
Ma non pulisce l'html come la funzione sopracitata.
Non c'è nessuno che può darmi una mano, gentilmente?