Created
July 7, 2015 12:53
-
-
Save cotcotquedec/9587d6d6a051a9ce49c0 to your computer and use it in GitHub Desktop.
Function de génération de tag HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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