note 49095 added to function.preg-replace
| From: | (jason) | Date: | Sun, 16 Jan 2005 17:26:00 +0000 |
| Subject: | note 49095 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83452@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49095
Delete -- http://master.php.net/manage/user-notes.php?action=delete+49095&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49095&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49095&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49095&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49095&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49095&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49095&report=yes
Search -- http://master.php.net/manage/user-notes.php