Skip to content

Instantly share code, notes, and snippets.

@zonuexe
Last active January 22, 2019 10:55
Show Gist options
  • Save zonuexe/a1ee5a136818a13c68e39a73423d6839 to your computer and use it in GitHub Desktop.
Save zonuexe/a1ee5a136818a13c68e39a73423d6839 to your computer and use it in GitHub Desktop.
どうしてもPHPからHTMLを出力せざるを得なくなったときにご利用ください
<?php
/**
* printf is template engine
*
* @license WTFPL http://www.wtfpl.net/
* @author USAMI Kenta <tadsan@zonu.me>
* @param string $format
* @param string|int|float $args...
* @return string
*/
function htmlsprintf($format)
{
return vsprintf($format, array_map(
function ($v) { return htmlspecialchars($v, ENT_QUOTES, 'UTF-8'); },
func_get_args()
));
}
<?php
$makelink = function ($id, $title) {
$url = sprintf('http://http://www.pixiv.net/member_illust.php?mode=medium&illust_id=%d', $id);
return htmlsprintf('<a href="%s">%d %s</a>', $id, $url, $title);
};
?>
<title>htmlsprintf</title>
<div>
<?= $makelink(40246869, "うきうき & ももんが") ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment