note 64364 deleted from function.preg-match by bjori
| From: | bjori@php.net | Date: | Thu, 13 Apr 2006 02:53:19 +0000 |
| Subject: | note 64364 deleted from function.preg-match by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-107953@lists.php.net to get a copy of this message | ||
Note Submitter: gedde at stud dot ntnu dot no
Reason: in docs
----
I was creating my own output buffering system to avoid problems with headers, and I thought I could
try to add automatic indentation to make my code more attractive to humans. And here it is. I have
to mention that it is for XHTML only, to ensure that simple ended tags like <img /> don't
cause any indentation. Works fine, but there are still room for a lot of improvements...
<?php
// Some variables
$stack = array();
$output = "";
$indent = -1;
//+-------------------------------------------
//| XHTML output buffer with indentation
//| @author: Mads Gedde <gedde@stud.ntnu.no>
//+-------------------------------------------
function add_html( $text ) {
global $output, $stack, $indent;
// Matches the first part of HTML-tags (for instance <table*> )
preg_match_all("/<([\w]+)[^>]*>/i", $text, $matches, PREG_PATTERN_ORDER);
// If we have matched a tag, let's indent
if (trim($matches[1][0]) != "") $indent++;
// We indent the number of times we have calculated
// Here I have chosen three spaces as the size, but you could also use \t for a tab
for ($i = 0; $i < $indent; $i++) $output .= " ";
// Then we add the text and a line break
$output .= $text . "\n";
// This checks if we have caught a tag name (for instance <table*> yells table
if (trim($matches[1][0]) != "") {
// Let's check if this is an simple ended tag <img /> or not
preg_match_all("/(<(". $matches[1][0] .")+([^>])*\/>)/i", $text,
$matches_new, PREG_PATTERN_ORDER);
if (trim($matches_new[1][0]) == "") {
// This is a not a simple ended tag, but a regular beginning tag
// Lets add this tag to the end of our stack to keep track of it
array_push($stack, $matches[1][0]);
} else {
// This is a simple ended tag, so we shall not search for an end tag
// Let us remove the indentation
$indent--;
}
}
// Gets the latest tag we caught from our stack
$latest_tag = array_pop($stack);
// Lets se if this text might have an end tag for us
preg_match_all("/<\/" . $latest_tag . ">/i", $text, $matches,
PREG_PATTERN_ORDER);
if (trim($matches[0][0]) != "") {
// We found an end tag, lets remove one level of indentation
$indent--;
} else {
// No end tag here, lets put the element back in the stack where it belongs
array_push($stack, $latest_tag);
}
}
?>
An example of the code and its use:
<?php
add_html( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
);
add_html( "<html xmlns=\"http://www.w3.org/1999/xhtml\">" );
add_html( "<head>");
add_html( "<title>My page</title>");
add_html( "<meta http-equiv='Content-Type' content='text/html;
charset=iso-8859-1' />" );
add_html( "<link rel='stylesheet' type='text/css'
href='style/original.css' />" );
add_html( "</head>");
add_html( "<body>" );
add_html( "<table width='100%' cellpadding='0' cellspacing='0'
border='0'>" );
add_html( "<tr>" );
add_html( "<td colspan='3' style='background: #034; color:
#fff;'>This is some text" );
add_html( "<a href='url.php'><img src='images/my-image.jpg'
alt='' /></a>" );
add_html( "</td>" );
add_html( "</tr>" );
add_html( "<tr>" );
add_html( "<td><a href='#'>My Link</a></td>" );
add_html( "<td>" );
add_html( "<img src='my-image.jpg' border='0' />" );
add_html( "</td>" );
add_html( "<td>Some text</td>" );
add_html( "</tr>" );
add_html( "</table>" );
add_html( "</body>" );
add_html( "</html>" );
echo $output;
?>
Works even better as part of an OO-based code optimized for output buffering...
¤ Gedde