html工具类收藏,自己封装的

<?


/**
 * html工具
 *
 */
include_once dirname(__FILE__) . '/stringutil.php';


class Util_Html {


    //static $htmlchar=array('&','<','>','"');
    static $html_decode_char = array('&amp;', '&lt;', '&gt;', '&quot;');
    static $html_encode_char = array('<', '>', '"'); //& 特殊处理


    /**
     * html 转义 函数
     * 支持 调用2次不重复转义
     * 测试用例
     * print Util_Html::attr("sdfksdfjsd f");
      echo "\n";
      $a='<br>';
      print Util_Html::encode($a)."\n";
      print Util_Html::encode(Util_Html::encode($a))."\n";


      print Util_Html::encode($a)."\n";
      print Util_Html::encode(Util_Html::encode(Util_Html::encode($a)))."\n";


      sdfksdfjsd f
      &lt;br&gt;
      &lt;br&gt;
      &lt;br&gt;
      &lt;br&gt;
     *
     * @param <type> $html
     */


    public static function encode($html) {
        // 特殊符号
        $t = 0;
        //编码前的符号看是否找到 需要编码
        foreach (self::$html_encode_char as $v) {
            if (stripos($html, $v) !== FALSE) {
                $t = 1;
                break;
            }
        }
        //可能有&  测试是否存在 &amp;。
        if (!$t) {
            //编码后的符号,没有没有找到 &amp; 之类需要编码 如果找到了就不编码了,避免重复编码
            foreach (self::$html_decode_char as $v) {
                if (stripos($html, $v) !== FALSE) {
                    $html = iconv("utf-8", "utf-8//IGNORE", $html);
                    return $html;
                }
            }
        }


        if ($t) {
            //存在需要编码的符号 而且 不存在 &amp;
            $html = htmlspecialchars($html);
        }
        $html = iconv("utf-8", "utf-8//IGNORE", $html);


        return $html;
    }


    /**
     * 对属性进行转义 去掉" 在用html转义进行转义 ,之后就不需要在进行 调用 htmlspecialchars 函数了
     * 比如:
     * <meta http-equiv="keywords" content="<?=Util_Html::attr('ddd"d')?>">
     * <meta http-equiv="description" content="<?=Util_Html::attr('ddd"d')?>">
     *
     * @param <type> $str
     */
    public static function attr($str) {
        $str = self::delChar($str);
        $str = strip_tags($str);
        return self::encode(str_replace('"', "", $str));
    }


    /**
     * 格式化 描述等大文本
     * @param <type> $html
     */
    public static function formatContent($html) {
        $html = self::delChar($html);
        return StringUtil::formatContent($html);
    }


    public static function urlStr($text) {
        return StringUtil::replaceUrlChar($text);
    }


    /**
     * 去掉特殊字符 不可见字符 在  htmlspecialchars
     * @param <type> $title
     * @return <type>
     */
    public static function formatTitle($title) {
        //还原字符
        //c3 a3  e2  82  ac c2 81
        /*
         * 处理流程: 比较下
         * echo "set names latin1;select productename from products_1 where id=33002878 " | mysql -usearch -psearchmc  ali | hex
          echo "set names utf8;select productename from products_1 where id=33002878 " | mysql -usearch -psearchmc  ali | hex
          吧16进制的数 改成 “ 使用\x转义即可
         */


        $title = strip_tags($title);
        $title = self::encode(self::delChar($title));
        return $title;
    }


    /**
     * 删除特殊字符 尽可能还原原始的字符
     * Util_Html::delChar
     * @param <type> $str
     * @return <type>
     */
    public static function delChar($str) {
        $str = iconv("utf-8", "utf-8//IGNORE", $str);
        return $str;
    }


    static $reg = array(
        'reg' => '[^ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[\\\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~
- 、ˉˇ¨‘’々~‖::∶”|〃〔〕《》「」『』.〖〗【】(){}ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ①②③④⑤⑥⑦⑧⑨⑩一二三四五六七八九十零≈≡≠=≤≥<>≮≯∷±+-×÷/∫∮∝∞∧∨∑∏∪∩∈∵∴⊥∠⌒⊙≌∽√°′$£¥‰%℃¤¢`!@#…&*—]+',
        'reg_schar' => '[、ˉˇ¨‘’々~‖::∶”’‘|〃〔〕《》「」『』.〖〗【【】()〔〕{} ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ①②③④⑤⑥⑦⑧⑨⑩一二三四五六七八九十零  ≈≡≠=≤≥<>≮≯∷±+-×÷/∫∮∝∞∧∨∑∏∪∩∈∵∴⊥‖∠⌒⊙≌∽√  °′〃$£¥‰%℃¤¢~`!@#¥%……&*()——+=-]\\s*[、ˉˇ¨‘’々~‖::∶”’‘|〃〔〕《》「」『』.〖〗【【】()〔〕{} ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ①②③④⑤⑥⑦⑧⑨⑩一二三四五六七八九十零  ≈≡≠=≤≥<>≮≯∷±+-×÷/∫∮∝∞∧∨∑∏∪∩∈∵∴⊥‖∠⌒⊙≌∽√  °′〃$£¥‰%℃¤¢~`!@#¥%……&*()——+=-]\\s*([、ˉˇ¨‘’々~‖::∶”’‘|〃〔〕《》「」『』.〖〗【【】()〔〕{} ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ①②③④⑤⑥⑦⑧⑨⑩一二三四五六七八九十零  ≈≡≠=≤≥<>≮≯∷±+-×÷/∫∮∝∞∧∨∑∏∪∩∈∵∴⊥‖∠⌒⊙≌∽√  °′〃$£¥‰%℃¤¢~`!@#¥%……&*()——+=-]\\s*)+',
    );


    public static function delChinese($content) {
        $content = mb_ereg_replace(self::$reg['reg'], '', $content);
        #连续3个汉字符号需要删除
        $content = mb_ereg_replace(self::$reg['reg_schar'], '', $content);
        return $content;
    }


    public static function substr($text, $start, $len) {
        if (function_exists('mb_substr')) {
            return mb_substr($text, $start, $len, 'utf-8');
        }
        return substr($text, $start, $len);
    }


    /**
     * 当内容在属性里面显示的时候
     * 去掉标签,去掉多余空格,转义双引号
     * @param <type> $html
     * @param <type> $maxlen
     * @param <type> $overtext
     * @return <type>
     */
    public static function attrContent($html, $maxlen=80, $overtext='') {
        //todo strip_tags 行为会把<p>dd</p><p>dd</p> => dddd
        $html = self::delChar($html);
        $text = trim(strip_tags($html));
        //去掉空格
        $text = str_ireplace(array('&nbsp;'), array(' '), $text);
        $text = preg_replace("/&nbsp;/", "", $text);
        $text = preg_replace('~\s+~ms', ' ', $text);
        $len = strlen($text);
        $word_code = array(ord('A'), ord('Z'), ord('0'), ord('9'), ord('a'), ord('z'));
        if ($len > $maxlen) {
            //截取不截断单词
            $is_cut = 0;
            $start_pos = $maxlen - 10 > 0 ? $maxlen - 10 : 0; //避免 有错误数据特长单词 数据被清空了,最多扫描10个字母
            for ($i = $maxlen; $i >= $start_pos; $i--) {
                $c = $text{$i};
                $c = self::substr($text, $i, 1);
                $c2 = ord($c);
                //非数字字母都是单词边界
                if (trim($c) == '') {
                    $text = self::substr($text, 0, $i + 1) . $overtext;
                    $is_cut = 1;
                    break;
                } elseif (!($c2 >= $word_code[0] && $c2 <= $word_code[1] ||
                        $c2 >= $word_code[2] && $c2 <= $word_code[3]
                        || $c2 >= $word_code[4] && $c2 <= $word_code[5])) {
                    // 其他字符的单词边界 不包括本身
                    $text = self::substr($text, 0, $i) . $overtext;
                    $is_cut = 1;
                    break;
                }
            }
            //没有边界的强制截断
            if (!$is_cut) {
                $text = self::substr($text, 0, $maxlen) . $overtext;
            }
        }
        $text = iconv("utf-8", "utf-8//IGNORE", $text);
        return htmlspecialchars(trim($text));
    }


    /**
     * 按照当前日期和页面参数得到一个随机数
     * 起源于:
     * 对于随机数的问题:


      1:当时出这个随机数,是为了能在不同的页面间出不同的词或者产品;
      2:静态页面可以,用随机数,因为最后静态化候都会同步到不同的机器上;
      3:加缓存的话,众多的站点容易产生过多的碎文件,如果缓存文件不同步,同样
      存在数据不统一;
      4:能否提取一个散列函数,按照当前日期和页面参数得到一个随机数;
     * @param <type> $start 起始值  闭区间 包含最大和最小值
     * @param <type> $end    最大值 闭区间 包含最大和最小值
     * @param <type> $date    日期的格式
     */
    public static function rand($start=0, $end=100, $date='Y-m-d') {
        $d = date($date);
        //命令行参数
        $arg = md5(print_r(isset($_SERVER['argv']) ? $_SERVER['argv'] : '', 1));
        $s2=$date.':'.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '' );
        //var_dump($s2);
        $s = md5($s2);
        //var_dump($s);
        $s = substr($s, 0, 6);
        $offset = abs($end - $start) + 1;
        return abs(hexdec($s)) % $offset + $start;
    }


    
    public static function _replace_email($arr){
        $mail=$arr[0];
        $t=explode('@', $mail);
        return str_repeat('*', strlen($t[0])).'@'.$t[1];//return '*'.substr($t[0],-2).'@'.$t[1];
    }
    public static function _replace_tel($arr){
        $tel=$arr[0];
        $n=6;
        if(strlen($tel)>$n){
            $tel=substr($tel, 0,-$n).str_repeat('*', $n);
        }
        return $tel;
    }


        /**
     * 格式化inquiry message 屏蔽 email 跟电话,保留格式
     * @param <type> $string
     */
    public static function messageFormat($string) {


        //$string=preg_replace_callback('~(\.\w+)+\@([A-Za-z0-9\-]+(\.))*[A-Za-z0-9]+~i', array('Util_Html','replace_email'), $string);
        $string=preg_replace_callback('~(\w+)@([A-Za-z0-9\-]+(\.))*[A-Za-z0-9]+~', array('Util_Html','_replace_email'), $string);
        $string=preg_replace_callback('~\+?([0-9]+(\s|\-))+[0-9]+~', array('Util_Html','_replace_tel'), $string);
        return $string;
    }


    /**
     * 检查2个urldomain是否一致
     * @param <type> $url1
     * @param <type> $url2
     */
    public static function sameDomain($url1, $url2) {
        //echo "test $url1,$url2 \n";
        if ($url1 == $url2) {
            return 1;
        }
        $info1 = @parse_url($url1);
        $info2 = @parse_url($url2);
        if (@$info2['host'] == @$info1['host']) {
            return 1;
        }
        //有1个为空 为0
        if (!@$info2['host'] || !@$info1['host']) {
            return 0;
        }
        //2个根域名
        $domain2 = explode("|", 'com.cn|net.cn|org.cn|gov.cn');


        $a1 = explode('.', strtolower($info1['host']));
        $a2 = explode('.', strtolower($info2['host']));
        //比较最小长度
        $len = min(count($a1), count($a2));
        $a1 = array_slice($a1, -$len);
        $a2 = array_slice($a2, -$len);
        $a1_test = array_slice($a1, -2);
        $test_len = 2;
        if (in_array(implode(".", $a1_test), $domain2)) {
            $test_len = 3;
        }


        $a1 = array_slice($a1, -$test_len);
        $a2 = array_slice($a2, -$test_len);


        return $a1 == $a2;
    }


    /**
     * 获取url的域名信息
     * @param <type> $url
     * @return <type>
     */
    public static function getDomain($url) {
        $info1 = @parse_url($url);
        //有1个为空 为0
        if (!@$info1['host']) {
            return '';
        }
        //2个根域名
        $domain2 = explode("|", 'com.cn|net.cn|org.cn|gov.cn');


        $a1 = explode('.', strtolower($info1['host']));
        //比较最小长度
        $a1_test = array_slice($a1, -2);
        $test_len = 2;
        if (in_array(implode(".", $a1_test), $domain2)) {
            $test_len = 3;
        }


        $a1 = array_slice($a1, -$test_len);


        return implode(".", $a1);
    }


    /**
     * 中文敏感词过滤
     * 一个简单的过滤器
     * @param <type> $word 
     */
    public static function filterCn($str){
        static $bad_words;
        $a=ord('a');
        $z=ord('z');
        $a0=ord('0');
        $a9=ord('9');
        if(!$bad_words){
            //第一次加载
            $bad_words=array();
            $tmps = explode("\n", file_get_contents(dirname(__FILE__) . '/bad_cn.txt')."\n".file_get_contents(dirname(__FILE__) . '/bad_cn_bbs.txt'));
            $tmps=array_map('trim',$tmps);
            $tmps=array_unique($tmps);
            foreach($tmps as $v){
                if($v){
                    $bad_words[]=$v;
                }
            }
        }
        foreach ($bad_words as $w){
            if(($i=stripos($str, $w))!==FALSE){
                //检测单词完整性
                if(preg_match('~[a-z\s0-9A-Z]+~i', $w) ){
                    if( $i>0){
                        $c=ord(strtolower(substr($str,$i-1,1)));


                        if($c>=$a && $c<=$z || $c>=$a0 && $c<=$a9 ){
                            continue;
                        }
                    }
                    if($i+strlen($w) <  strlen($str)){
                        $c=ord(strtolower(substr($str,$i+strlen($w),1)));                       
                        if($c>=$a && $c<=$z || $c>=$a0 && $c<=$a9 ){
                            continue;
                        }
                    }
                }
                return false;
            }
        }
        return true;
    }
    public static function filterWord($word) {
        $word = str_replace('_', ' ', $word);
        $word = strtolower(trim($word));
        if (!$word) {
            return;
        }
        $words = explode("\n", file_get_contents(dirname(__FILE__) . '/bad_word.txt'));
        foreach ($words as $w) {
            //包含关系
            if (stripos($word, $w) !== false) {
                header("location:/");
                echo 'bad word';
                exit;
            }
        }
    }


    /**
     * 高亮关键词
     * @param <type> $str
     * @param <type> $keystr
     * @return <type>
     */
    public static function hightlight($str, $keystr) {
        $s = explode(" ", $str);
        $ks = array();
        foreach (explode(" ", $keystr) as $t) {
            $ks[$t] = 1;
        }
        foreach ($s as $k => $v) {
            $v2 = strtolower($v);
            if (isset($ks[$v2])) {
                $s[$k] = '<font style="color:red;font-weight:bold">' . htmlspecialchars($v) . '</font>';
            }
        }
        return implode(" ", $s);
    }


    /**
     * 对url 进行隐藏
     * @param <type> $url
     */
    public static function hideUrl($url,$max_len=70) {
        //这个函数总报错?
        if(!$url){
            return '';
        }
        $r = preg_match('~((http(s*)://([^/]+)))(.*)~i', $url, $arr2);
        if ($r) {
            $path = $arr2[5];
            if (strlen($path) > 50) {
                $path = '/...' . substr($path, -47);
            }
            $url2 = $arr2[1] . $path;


            
        } else {


            $arr=@parse_url($url);
            if($arr){
                $path=$arr['path'];
                if (strlen($path) > 10) {
                    $path = '/...' . substr($path, -7);
                }
                $url2 = 'http://'.$arr['host'] . $path;
             
            }else{
                $url2= Util_Html::attrContent($url,50,'...');
            }
        }
        if(strlen($url2)>$max_len){
            $url2=substr($url2,0,$max_len)."\n".substr($url2,$max_len);
        }
        return $url2;
    }
    /**
     * 生成md5签名
     * 1.字母升序排列参数
     * 2.md5大写返回
     * 
     * @param <type> $url
     * @return <type> 
     */
    public static function sign_key($url) {
        if(empty ($url)) {
            return ;
        }
        $key = 'aoIdfh59*%_#{:)';
        $pares_url = parse_url($url);
        parse_str($pares_url['query'],$querys);
        $new_url = '';
        if(!empty($querys)) {
            $new_url = '';
            unset ($querys['key']);
            ksort($querys);
            foreach($querys as $key => $query) {
                $new_url .= $key.$query;
            }
            $new_url = strtoupper(md5($new_url.$key));
        }
        return $new_url;
    }


    public static function sign_str($str){
        $key = 'aoIdfh59*%_#{:)';
        return md5($str.$key) ;
    }


    /**
  * 解析HTML为UBB函数
  *
  * @param string $str 要解析的字符串
  * @return string
  */
    public static function toUbb($html){


    $output=$html;
    $output=preg_replace('/\r/i',"",$output);
    $output=preg_replace('/\r/i',"",$output);
    $output=preg_replace('/on(load|click|dbclick|mouseover|mousedown|mouseup)="[^"]+"/i',"",$output);
    $output=preg_replace('/<script[^>]*?>([\w\W]*?)<\/script>/i',"",$output);


    $output=preg_replace('/<a[^>]+href="([^"]+)"[^>]*>(.*?)<\/a>/i',"\n[url=$1]$2[/url]\n",$output);


    $output=preg_replace('/<font[^>]+color=([^ >]+)[^>]*>(.*?)<\/font>/i',"[color=$1]$2[/color]",$output);


    $output=preg_replace('/<img[^>]+src="([^"]+)"[^>]*>/i',"\n[img]$1[/img]\n",$output);


    $output=preg_replace('/<([\/]?)b>/i',"[$1b]",$output);
    $output=preg_replace('/<([\/]?)strong>/i',"[$1b]",$output);
    $output=preg_replace('/<([\/]?)u>/i',"[$1u]",$output);
    $output=preg_replace('/<([\/]?)i>/i',"[$1i]",$output);


    $output=preg_replace('/&nbsp;/i'," ",$output);
    $output=preg_replace('/&amp;/i',"&",$output);
    $output=preg_replace('/&quot;/i',"\"",$output);
    $output=preg_replace('/&lt;/i',"<",$output);
    $output=preg_replace('/&gt;/i',">",$output);


    $output=preg_replace('/<br>/i',"\n",$output);
    $output=preg_replace('/<[^>]*?>/i',"",$output);
    $output=preg_replace('/\[url=([^\]]+)\]\n(\[img\][^\[]+?\[\/img\])\n\[\/url\]/i',"[url=$1]$2[/url]",$output);
    $output=preg_replace('/\n+/i',"\n",$output);


    return $output;




}


/**
 * 获取友好的时间
 * @param <type> $strtime
 * @param <type> $lang
 * @return <type>
 */
function offsettime($strtime, $lang='en')
{
  $curtime = time();
  $time = strtotime($strtime);


  $span = $curtime - $time;
  if($span <= 0)
  {
    if($lang == 'zh')
      return "1秒前";
    return "1 second ago";
  }
  else if($span < 60)
  {
    if($lang == 'zh')
      return $span."秒前";
    return $span." seconds ago";
  }
  else if($span <= 120)
  {
    if($lang == 'zh')
      return "1分钟前";
    return "1 minute ago";
  }
  else if($span < 3600)
  {
    $m = (int)($span / 60);
    if($lang == 'zh')
      return $m."分钟前";
    return $m." minutes ago";
  }
  else if($span <= 7200)
  {
    if($lang == 'zh')
      return "1小时前";
    return "1 hour ago";
  }
  else if($span < 86400)
  {
    $h = (int)($span / 3600);
    if($lang == 'zh')
      return $h."小时前";
    return $h." hours ago";
  }
  else if($span < 172800)
  {
    if($lang == 'zh')
      return "1天前";
    return "1 day ago";
  }
  else
  {
    $d = (int)($span / 86400);
    if($lang == 'zh')
      return $d."天前";
    return $d." days ago";
  }
}
}


if (function_exists('mb_regex_encoding')) {
    mb_internal_encoding("UTF-8");
    mb_regex_encoding("UTF-8");
}
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值