-
-
Save NorihikoMoriyama/2fef74b9da52de4d933fd27f4024fa87 to your computer and use it in GitHub Desktop.
PHPで <a> タグ以外をhtmlspecialcharsする関数
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> </a> タグ以外をhtmlspecialcharsする関数 | |
function atagigaiwohsc($src,$flags = ENT_QUOTES){ | |
// encode including <a></a> | |
$sanitized = htmlspecialchars($src,$flags); | |
// decode all <a> | |
if(preg_match_all('@<a.*?>@', $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('@</a>@','</a>',$sanitized); | |
return $sanitized; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment