Skip to content

Instantly share code, notes, and snippets.

@cotcotquedec
Created July 7, 2015 12:53
Show Gist options
  • Save cotcotquedec/9587d6d6a051a9ce49c0 to your computer and use it in GitHub Desktop.
Save cotcotquedec/9587d6d6a051a9ce49c0 to your computer and use it in GitHub Desktop.
Function de génération de tag HTML
<?php
/**
* Génération d'une balise html
*
* ex :
* html('a', ['href' => '/my/page', 'tutu' => 'tata'], 'Let\'s go')
* => <a href="/my/page" tutu="tata" />Let's go</a>
*
* @param $tag
* @param array $attributes
* @param string $content
* @return string
*/
function html($tag, $attributes = [], $content = '')
{
$autoclosed = [
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
];
// gestion des attributs
foreach($attributes as $key => &$value) {
$value = sprintf('%s="%s"', $key, str_replace('"','\"', $value)) . ' ';
}
$attributes = implode(' ', $attributes);
return array_search($tag, $autoclosed) === false ? sprintf('<%s %s/>%s</%1$s>', $tag, $attributes, $content) : sprintf('<%s %s/>', $tag, $attributes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment