当前位置: 移动技术网 > IT编程>开发语言>PHP > php中将字符串转为HTML的实体引用的一个类

php中将字符串转为HTML的实体引用的一个类

2019年04月14日  | 移动技术网IT编程  | 我要评论

龙魔传说txt下载,高速相机,火蝴蝶第二部

复制代码 代码如下:

class htmlencode {
        static $_converttohtmlentitiessrcencoding='utf-8';

        /**
         * 将非ascii字符串转换成html实体
         *
         * @example htmlencode::encode("我信了"); //输出:我信了
         * @param string $s 要进行编码的字符串
         * @return string 返回html实体引用
         */
        public static function encode($s,$srcencoding='utf-8') {
            self::$_converttohtmlentitiessrcencoding=$srcencoding;
            return preg_replace_callback('|[^\x00-\x7f]+|',array(__class__,'_converttohtmlentities'),$s);
        }

        public static function _converttohtmlentities($data) {
            if (is_array($data)) {
                $chars=str_split(iconv(self::$_converttohtmlentitiessrcencoding,"ucs-2be",$data[0]),2);
                $chars=array_map(array(__class__,__function__),$chars);
                return join("",$chars);
            } else {
                $code=hexdec(sprintf("%02s%02s;",dechex(ord($data {0})),dechex(ord($data {1}))));
                return sprintf("&#%s;",$code);
            }
        }     
    }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网