Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PHPで <a> タグ以外をhtmlspecialcharsする関数 何の役に立つか?つまんねー事聞くなよ!
// <a> </a> タグ以外をhtmlspecialcharsする関数
function atagigaiwohsc($src,$flags = ENT_QUOTES){
// encode including <a></a>
$sanitized = htmlspecialchars($src,$flags);
// decode all <a>
if(preg_match_all('@&lt;a.*?&gt;@', $sanitized, $matches, PREG_SET_ORDER)){
foreach($matches as $match){
$sanitized = preg_replace('@'.preg_quote($match[0],'@').'@', htmlspecialchars_decode($match[0],$flags), $sanitized);
// decode </a>
$sanitized = preg_replace('@&lt;/a&gt;@','</a>',$sanitized);
return $sanitized;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment