不适用于表情符号的htmlentites
原学程将引见没有实用于脸色标记的htmlentites的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我正在测验考试显示字符html虚体
echo htmlentities(htmlentities("&"));
//outputs &
echo htmlentities(htmlentities("<"));
//outputs <
但是它仿佛没有实用于脸色标记
echo htmlentities(htmlentities("?"));
//outputs ?
怎样使其输入&#一二8五二六;
?
编纂:
我正在测验考试显示用户输出的字符串,并对于一切html虚体停止编码。
echo htmlentities(htmlentities($input))
示例:
"this & that ?" -> "this & that &#一二8五二六;"
推举谜底
这实用于惯例的Htf虚体、Utf⑻脸色标记(以及其余utf实质),固然也实用于惯例字符串。
我只是在处置空字符串值时碰到了成绩,所以我必需将此前提搁进函数中。
function entities( $string ) {
$stringBuilder = "";
$offset = 0;
if ( empty( $string ) ) {
return "";
}
while ( $offset >= 0 ) {
$decValue = ordutf8( $string, $offset );
$char = unichr($decValue);
$htmlEntited = htmlentities( $char );
if( $char != $htmlEntited ){
$stringBuilder .= $htmlEntited;
} elseif( $decValue >= 一二8 ){
$stringBuilder .= "&#" . $decValue . ";";
} else {
$stringBuilder .= $char;
}
}
return $stringBuilder;
}
// source - http://php.net/manual/en/function.ord.php#一0九8一二
function ordutf8($string, &$offset) {
$code = ord(substr($string, $offset,一));
if ($code >= 一二8) { //otherwise 0xxxxxxx
if ($code < 二二四) $bytesnumber = 二; //一一0xxxxx
else if ($code < 二四0) $bytesnumber = 三; //一一一0xxxx
else if ($code < 二四8) $bytesnumber = 四; //一一一一0xxx
$codetemp = $code - 一九二 - ($bytesnumber > 二 ? 三二 : 0) - ($bytesnumber > 三 ? 一六 : 0);
for ($i = 二; $i <= $bytesnumber; $i++) {
$offset ++;
$code二 = ord(substr($string, $offset, 一)) - 一二8; //一0xxxxxx
$codetemp = $codetemp*六四 + $code二;
}
$code = $codetemp;
}
$offset += 一;
if ($offset >= strlen($string)) $offset = ⑴;
return $code;
}
// source - http://php.net/manual/en/function.chr.php#88六一一
function unichr($u) {
return mb_convert_encoding('&#' . intval($u) . ';', 'UTF⑻', 'HTML-ENTITIES');
}
/* ---- */
var_dump( entities( "&" ) ) . "
";
var_dump( entities( "<" ) ) . "
";
var_dump( entities( "?" ) ) . "
";
var_dump( entities( "☚" ) ) . "
";
var_dump( entities( "" ) ) . "
";
var_dump( entities( "A" ) ) . "
";
var_dump( entities( "Hello ? world" ) ) . "
";
var_dump( entities( "this & that ?" ) ) . "
";
佳了闭于没有实用于脸色标记的htmlentites的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。