Miscellaneous Code

  Home arrow Miscellaneous Code arrow xhtml/xml indenter
MISCELLANEOUS CODE

xhtml/xml indenter
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-04-23

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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
     tags, so beware.

    By : Venti

    <?php
    /*******************************************************************************

    xhtml indenter

    http://ventionline.com

    Copyright (C) 2003 Ari Koivula (Venti) - venti@ventionline.com

    *******************************************************************************/

    /*******************************************************************************

    indent_xhtml(string &$source, string $indenter = ' ')

    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.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap