Aggiungo anche queste due funzioni php per eliminare gli spazi utilizzati di solito per indentare il codice nelle pagine html (sono prese da Smarty, non le ho scritte io):
http://forum.html.it/forum/showthrea...0#post11918240
esempio di utilizzo (NON utilizzatelo su siti trafficati se non effettuate il caching delle pagine sul server!):
Create una nuova pagina provapagina.php e inseriteci dentro questo codice:
e, guardando nel sorgente della pagina, vedrete come l'indentazione sia stata rimossa automaticamente dall'html.Codice PHP:<?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;
}
ob_start(); // non usate ob_start('ob_gz_handler'); o non funzionerà
?>
Mettete
qui
il
codice
HTML
indentandolo
per
bene
per vedere
l'effetto.
<?php
$html = trimwhitespace(ob_get_clean());
echo $html;
exit();
?>
Per vedere meglio l'effetto, provate a cambiare queste righe:
così:Codice PHP:<?php
$html = trimwhitespace(ob_get_clean());
echo $html;
exit();
?>
e aprite la pagina prima così:Codice PHP:<?php
if(empty($_GET['nospazi'])) $_GET['nospazi'] = 'no';
if($_GET['nospazi'] == 'yes'){
$html = trimwhitespace(ob_get_clean());
echo $html;
}
exit();
?>
http://vostrosito.ext/pagina.php
e poi così:
http://vostrosito.ext/pagina.php?nospazi=yes
guardando la differenza tra i sorgenti.
Ciao![]()

Rispondi quotando