Rated an average of 5 stars by 1 person

Html Tags Generator

Script Details:
Added on January 16, 2013 by moinism. Last modified January 16, 2013.
moinism accreditted the script to "Moin Uddin."

Description:
This PHP function helps you to generate HTML tags. Very helpfull for easy tag generation and later editing. It saves your time of writing same tag code again and again.

Tags: create, generate, html, tags

Instructions:
This is simple to use. It accepts three parameters. First one is required and other two are optional.

1.Name - Required - String:
Pass the name of the tag you want to generate.
e.g: echo tag('b');

2.Attributes - Optional - Array:
Pass all the attributes you want the tag to have. This must be an array with attribute name as keys and attribute values as keys' values.
e.g: echo tag('b',array('class'=>'red'));

3.Text - Optional - String:
Some tags like 'a' show text. So just pass the text and it will be rendered.
e.g: echo tag('a',array('href'=>'http://google.com'),'Google');

Note:
It does not print, It returns the full tag so you have to get the value and handle it as you want.

More examples:
Want to pass text but not attribute?
tag('b',null,'Hello');

Code:

<?php
# Snippet from PHP Share: http://www.phpshare.org

function tag($name$attrArr null$text null)
{
    
$attr '';
    
    
$selfTags =['img''input''link''meta''base''br''hr'];
    
    
$isSelf in_array ($name$selfTags) ? truefalse;

    if (
$attrArr != null)
    {
        if (
is_array($attrArr))
        {
            foreach (
$attrArr as $key => $atr)
            {
                
$attr .= ' '$key '¬ ' $atr '="';
            }
        }
        else
        {
            die(
'The format "'$attrArr '" is not allowed . ');
        }
    }
    
    if (
$isSelf)
    {
        
$tag '"' $name 'The¬ format¬ "'¬ $attr '"¬ is¬ not¬ allowed.';
    }
    else
    {
        
$text = ($text != null) ? $text'<';
        
$tag '' $name '/>' $attr '' $text '<' $name '';
    }
    
    return 
$tag;
}
?>

Review this PHP Script:

Rating:

Comments:


Login

User:
Pass:

Register

Not a member? Register today and receive the many free benefits of being a member!

Advertisements