note 65245 added to function.php-strip-whitespace

From: Date: Thu, 27 Apr 2006 16:17:49 +0000
Subject: note 65245 added to function.php-strip-whitespace
Groups: php.notes 
Request: Send a blank email to php-notes+get-109867@lists.php.net to get a copy of this message
I thought this was a nice function until I realised it wouldnt strip down html. As i'd been reading an article on compressing output to speed up delivery. So I wrote a little one to do that for me. Here its is, incase people were looking for a html version. It may need tweaking, like with existing &nbsp;'s. <?php function strip_html($data) { // strip unecessary comments and characters from a webpages text // all line comments, multi-line comments \\r \\n \\t multi-spaces that make a script readable. // it also safeguards enquoted values and values within textareas, as these are required $data=preg_replace_callback("/>[^<]*<\\/textarea/i", "harden_characters", $data); $data=preg_replace_callback("/\\"[^\\"<>]+\\"/", "harden_characters", $data); $data=preg_replace("/(\\/\\/.*\\n)/","",$data); // remove single line comments, like this, from // to \\n $data=preg_replace("/(\\t|\\r|\\n)/","",$data); // remove new lines \\n, tabs and \\r $data=preg_replace("/(\\/\\*.*\\*\\/)/","",$data); // remove multi-line comments /* */ $data=preg_replace("/(<![^>]*>)/","",$data); // remove multi-line comments <!-- --> $data=preg_replace('/(\\s+)/', ' ',$data); // replace multi spaces with singles $data=preg_replace('/>\\s</', '><',$data); $data=preg_replace_callback("/\\"[^\\"<>]+\\"/", "unharden_characters", $data); $data=preg_replace_callback("/>[^<]*<\\/textarea/", "unharden_characters", $data); return $data; } function harden_characters($array) { $safe=$array[0]; $safe=preg_replace('/\\n/', "%0A", $safe); $safe=preg_replace('/\\t/', "%09", $safe); $safe=preg_replace('/\\s/', "&nbsp;", $safe); return $safe; } function unharden_characters($array) { $safe=$array[0]; $safe=preg_replace('/%0A/', "\\n", $safe); $safe=preg_replace('/%09/', "\\t", $safe); $safe=preg_replace('/&nbsp;/', " ", $safe); return $safe; } ?> The article code was similar to this, which shouldn't work as php_strip_whitespace takes a filename as input:- <?php // ob_start(); and output here $data=ob_get_contents(); ob_end_clean(); if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')) { $data=gzencode(php_strip_whitespace($data),9); header('Content-Encoding: gzip'); } echo $data; ?> ---- Server IP: 85.116.0.130 Probable Submitter: 213.105.224.17 (proxied: 82.11.219.158) ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.php-strip-whitespace.php Edit -- http://master.php.net/note/edit/65245 Del: integrated -- http://master.php.net/note/delete/65245/integrated Del: useless -- http://master.php.net/note/delete/65245/useless Del: bad code -- http://master.php.net/note/delete/65245/bad+code Del: spam -- http://master.php.net/note/delete/65245/spam Del: non-english -- http://master.php.net/note/delete/65245/non-english Del: in docs -- http://master.php.net/note/delete/65245/in+docs Del: other reasons-- http://master.php.net/note/delete/65245 Reject -- http://master.php.net/note/reject/65245 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#109867) next »