note 49095 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jun 2006 13:22:45 +0000 |
| Subject: | note 49095 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114332@lists.php.net to get a copy of this message | ||
Note Submitter: jason at thinkingman dot org (jason)
----
Strip all whitespace around <TAGS>.
This is useful for those who want to reduce the size of their web pages by eliminating all
unnecessary whitespace (\r\n\t, etc...). This code only removes the whitespace directly around a
<TAG>, not the whitespace within a tag or within the string output of a tag.
For example:
<?
ob_start();
$str = " This is some text ";
?>
<html>
<head>
<title>My Fantastic Website! </title>
</head>
<body>
<?=$str?>
</body>
</html>
<?
$output = preg_replace("/(\s+)?(\<.+\>)(\s+)?/", "$2",
ob_get_contents());
ob_end_clean();
echo $output;
?>
The output will be (instead of formatted HTML, one nice long line of HTML, minus all unnecessary
whitespace characters):
<html><head><title>My Fantastic
Website!</title></head><body>This is some text</body></html>
Although a little-bit of overhead (and I do mean *little* to non-existent) is incurred, the BYTE
savings can be extremely valuable; unnecessary code isn't transmitted over the line, your code
becomes tight and clean (albeit, not desirably readable to anyone viewing your source), and pages
download quicker, so on...
Use wisely.
Jason