Last active
September 15, 2019 04:37
-
-
Save stemar/5188809 to your computer and use it in GitHub Desktop.
A PHP function to create an <a> link and a callback to join HTML tag attributes.
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 | |
function a($href, $text=NULL, $attributes=NULL) { | |
if (!isset($text)) | |
$text = strpos((string)$href, 'mailto:') !== FALSE ? substr($href, 7) : $href; | |
$attributes = compact('href') + (array)$attributes; | |
return sprintf('<a%s>%s</a>', join_attributes($attributes), (string)$text); | |
} | |
function join_attributes($attributes) { | |
$attrs = array(); | |
foreach ((array)$attributes as $key => $value) | |
if ($key === 'value' || !is_blank($value)) | |
$attrs []= sprintf(' %s="%s"', $key, (string)$value); | |
return join($attrs); | |
} | |
function is_blank($value) { | |
return empty($value) && !is_numeric($value); | |
} |
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 | |
a('http://google.com', 'Google', array('title'=>'Search with Google', 'class'=>'no_underline')); | |
// => <a href="http://google.com" title="Search with Google" class="no_underline">Google</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment