php 实体解码,如何解码PHP中的数字HTML实体

以下代码片段(大部分从

here被盗,改进后)将适用于文字,数字十进制和数字十进制十进制实体:

header("content-type: text/html; charset=utf-8");

/**

* Decodes all HTML entities, including numeric and hexadecimal ones.

*

* @param mixed $string

* @return string decoded HTML

*/

function html_entity_decode_numeric($string, $quote_style = ENT_COMPAT, $charset = "utf-8")

{

$string = html_entity_decode($string, $quote_style, $charset);

$string = preg_replace_callback('~([0-9a-fA-F]+);~i', "chr_utf8_callback", $string);

$string = preg_replace('~([0-9]+);~e', 'chr_utf8("\\1")', $string);

return $string;

}

/**

* Callback helper

*/

function chr_utf8_callback($matches)

{

return chr_utf8(hexdec($matches[1]));

}

/**

* Multi-byte chr(): Will turn a numeric argument into a UTF-8 string.

*

* @param mixed $num

* @return string

*/

function chr_utf8($num)

{

if ($num < 128) return chr($num);

if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);

if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);

if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);

return '';

}

$string ="”";

echo html_entity_decode_numeric($string);

欢迎改进建议.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值