This function is used for indenting (formatting) a messy target xhtml page into a properly indentet page. It requires all the tags in the page to be properly closed. It also formats the stuff between
Function description:
This function is used for indenting (formatting) a messy target xhtml page
into a properly indentet page.
It requires all the tags in the page to be properly closed.
It also formats the stuff between <pre> tags, so beware.
$source
This variable has the original xhtml/xml source in it.
By default its included by reference, but you can take off the & and uncomment
the return line at the bottom to make it return the indentet string without
modifying the old one.
$indenter
This variable is the indenter character(s) used for identing (duh).
example:
indent_xhtml($source, $indenter = ' ');
+-------------------+
|<html> |
| <head> |
| <title> |
+-------------------+
indent_xhtml($source, $indenter = ' ');
+-------------------+
|<html> |
| <head> |
| <title> |
+-------------------+
*******************************************************************************/
function indent_xhtml(&$source, $indenter = ' ') {
// Remove all pre-existing formatting.
// Remove all newlines.
$source = str_replace("\n", '', $source);
$source = str_replace("\r", '', $source);
// Remove all tabs.
$source = str_replace("\t", '', $source);
// Remove all space after ">" and before "<".
$source = ereg_replace(">( )*", ">", $source);
$source = ereg_replace("( )*<", "<", $source);
// Iterate through the source.
$level = 0;
$source_len = strlen($source);
$pt = 0;
while ($pt < $source_len) {
if ($source{$pt} === '<') {
// We have entered a tag.
// Remember the point where the tag starts.
$started_at = $pt;
$tag_level = 1;
// If the second letter of the tag is "/", assume its an ending tag.
if ($source{$pt+1} === '/') {
$tag_level = -1;
}
// If the second letter of the tag is "!", assume its an "invisible" tag.
if ($source{$pt+1} === '!') {
$tag_level = 0;
}
// Iterate throught the source until the end of tag.
while ($source{$pt} !== '>') {
$pt++;
}
// If the second last letter is "/", assume its a self ending tag.
if ($source{$pt-1} === '/') {
$tag_level = 0;
}
$tag_lenght = $pt+1-$started_at;
// Decide the level of indention for this tag.
// If this was an ending tag, decrease indent level for this tag..
if ($tag_level === -1) {
$level--;
}
// Place the tag in an array with proper indention.
$array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
// If this was a starting tag, increase the indent level after this tag.
if ($tag_level === 1) {
$level++;
}
// if it was a self closing tag, dont do shit.
}
// Were out of the tag.
// If next letter exists...
if (($pt+1) < $source_len) {
// ... and its not an "<".
if ($source{$pt+1} !== '<') {
$started_at = $pt+1;
// Iterate through the source until the start of new tag or until we reach the end of file.
while ($source{$pt} !== '<' && $pt < $source_len) {
$pt++;
}
// If we found a "<" (we didnt find the end of file)
if ($source{$pt} === '<') {
$tag_lenght = $pt-$started_at;
// Place the stuff in an array with proper indention.
$array[] = str_repeat($indenter, $level).substr($source, $started_at, $tag_lenght);
}
// If the next tag is "<", just advance pointer and let the tag indenter take care of it.
} else {
$pt++;
}
// If the next letter doesnt exist... Were done... well, almost..
} else {
break;
}
}
// Replace old source with the new one we just collected into our array.
$source = implode($array, "\n");
// return $source;
}
?>
DISCLAIMER:
The content provided in this article is not warranted or guaranteed by Developer Shed, Inc.
The content provided is intended for entertainment and/or educational purposes in order to
introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent
upon the reader to employ real-world tactics for security and implementation of best practices.
We are not liable for any negative consequences that may result from implementing any information
covered in our articles or tutorials. If this is a hardware review, it is not recommended to open
and/or modify your hardware.