Skip to content

Instantly share code, notes, and snippets.

@oh-sky
Created April 26, 2013 03:28
Show Gist options
  • Save oh-sky/5464877 to your computer and use it in GitHub Desktop.
Save oh-sky/5464877 to your computer and use it in GitHub Desktop.
PHPで <a> タグ以外をhtmlspecialcharsする関数 何の役に立つか?つまんねー事聞くなよ!
<?php
// <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