Really boring HTML & XML tag class function for PHP

That’s right. It’s simple boring and reusable. I used it for the complex HTML need to make the WP Super Edit WordPress administration options. It’s actually a function, but I use it inside a PHP class when I know I’m going to be either doing nasty HTML echo’s or returning complex HTML formatted variables. It’s not a template system or anything like that, I just needed something without the massive dependencies and overhead of a full fledged HTML / Dom / Pear class.

I’ve put it into a simple class for this example, and you can download the text file in case my site mangles the code: Real Boring HTML & XML Class PHP Text File

Oh yea, and since a tag is a tag, then this can also do simple XML.

class boring_tag_creator {

     * Display or return html or xml tag with attributes.
     * @param array $tag_options options and content to display
     * @return mixed
     function the_tag( $tag_options = array() ) {
         $attributes = '';
         $composite = '';
         foreach ( $tag_options as $name => $option ) {
             if ( $name == 'tag' ) continue;
             if ( $name == 'content' ) continue;
             if ( $name == 'return' ) continue;
             if ( $name == 'tag_type' ) continue;
             $tag_attributes .= sprintf( ' %s="%s"', $name, $option );
         switch ( $tag_options['tag_type'] ) {
             case 'single':
                 $format = '%3$s <%1$s%2$s />' ;
             case 'single-after':
                 $format = '<%1$s%2$s /> %3$s' ;
             case 'open':
                 $format = '<%1$s%2$s>%3$s';
             case 'close':
                 $format = '%3$s<!--%1$s-->';
                 $format = '<%1$s%2$s>%3$s<!--%1$s-->';
         $composite = sprintf( $format, $tag_options['tag'], $tag_attributes, $tag_options['content'] );
         if ( $tag_options['return'] == true ) return $composite ;
         echo $composite;
     * Boring Header and paragraph either echoed or returned...
     function boring_html( $header, $content, $return = false ) {
         $the_h2 = array(
             'tag' => 'h2',
             'content' => $header
         $the_content = array(
             'tag' => 'p',
             'id' => 'boring_content_id',
             'style' => 'border: 1px solid red',
             'class' => 'boring_content_class',
             'content' => $content
         if ( $return ) {
             $the_h2[ 'return' ] = true;
             $the_content[ 'return' ] = true;
             return $this->the_tag( $the_h2 ) . $this->the_tag( $the_content );
         } else {
             $this->the_tag( $the_h2 );
             $this->the_tag( $the_content );


$boring = new boring_tag_creator;

$boring->boring_html( 'The Title', 'Some words about this for the content.' );

$boring_html_variable = $boring->boring_html( 'Another Title', 'More words about this other thing for the content.', true );

echo $boring_html_variable;

The class function the_tag takes an array. In that array there are only three special keys.

  • tag (the html or xml tag)
  • content (the content that goes somewhere with in or near the tag see tag_type)
  • return ( To return everything as a string set as true, or to echo set as false which is the default  )
  • tag_type ( controls how the tag is composed )
    • single ( put the content in front and self close the tag )
    • single-after ( put the content behind and self close the tag )
    • open ( create an open tag and put content after )
    • close ( close the tag )
    • default ( opened the tag, put the content, close the tag )

Whatever else you put as part of the array becomes an attribute for the tag. Simple, boring, and might be useful. Like I said, boring, but this has been useful for me to construct some complex HTML and XML structures.

2 thoughts on “Really boring HTML & XML tag class function for PHP

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.