Home > php教程 > PHP源码 > body text

分享一个PHP 网站SEO及过滤类,方便使用

PHP中文网
Release: 2016-05-25 17:11:53
Original
1017 people have browsed it

在给网站页面做SEO的时候,长用到得一些函数。 
比如:良好URL生成,用户名过滤,email检测,截取字符串,全角替换成半角,删除html等 

1. [PHP]代码

<?php
 
class SeoTools
{
    public function friendlyURL($string, $replacement = &#39;-&#39;) {
        $map = array(
            &#39;/à|á|å|â|ä/&#39; => &#39;a&#39;,
            &#39;/è|é|ê|ẽ|ë/&#39; => &#39;e&#39;,
            &#39;/ì|í|î/&#39; => &#39;i&#39;,
            &#39;/ò|ó|ô|ø/&#39; => &#39;o&#39;,
            &#39;/ù|ú|ů|û/&#39; => &#39;u&#39;,
            &#39;/ç|č/&#39; => &#39;c&#39;,
            &#39;/ñ|ň/&#39; => &#39;n&#39;,
            &#39;/ľ/&#39; => &#39;l&#39;,
            &#39;/ý/&#39; => &#39;y&#39;,
            &#39;/ť/&#39; => &#39;t&#39;,
            &#39;/ž/&#39; => &#39;z&#39;,
            &#39;/š/&#39; => &#39;s&#39;,
            &#39;/æ/&#39; => &#39;ae&#39;,
            &#39;/ö/&#39; => &#39;oe&#39;,
            &#39;/ü/&#39; => &#39;ue&#39;,
            &#39;/Ä/&#39; => &#39;Ae&#39;,
            &#39;/Ü/&#39; => &#39;Ue&#39;,
            &#39;/Ö/&#39; => &#39;Oe&#39;,
            &#39;/ß/&#39; => &#39;ss&#39;,
            &#39;/ /&#39;=>&#39; &#39;,
            &#39;/ /&#39;=>&#39;&#39;,
       &#39;/~|·|!|@|#|¥|%|…|&|×|(|)|-|\+|=|『|【|』|】|、|:|;|“|”|\&#39;|
       《|,|》|。|?|\/|—|_|:|√|<|°|丶|>|-|★|||│|‖|ˇ/&#39;=>&#39; &#39;,
            &#39;/[^\w\s\x80-\xff]/&#39; => &#39; &#39;,
            &#39;/\\s+/&#39; => $replacement
        );
        $string = trim($string);
        $string = preg_replace(array_keys($map), array_values($map), $string);
        $string = preg_replace(&#39;/\\s+/&#39;,$replacement, strtolower($string));
        $string = trim($string,$replacement);
        return $string;
    }
     
    //判断email
    public function isEmail($mailAddr)
    {
        return strlen($mailAddr) > 6 && preg_match("/^[\w\-\.]+@[\w\-]+(\.\w+)+$/", $mailAddr);
 
    }
    //过滤用户
    public function CheckUser($string,$replacement = &#39;_&#39;)
    {
        $map = array(
            &#39;/à|á|å|â|ä/&#39; => &#39;a&#39;,
            &#39;/è|é|ê|ẽ|ë/&#39; => &#39;e&#39;,
            &#39;/ì|í|î/&#39; => &#39;i&#39;,
            &#39;/ò|ó|ô|ø/&#39; => &#39;o&#39;,
            &#39;/ù|ú|ů|û/&#39; => &#39;u&#39;,
            &#39;/ç|č/&#39; => &#39;c&#39;,
            &#39;/ñ|ň/&#39; => &#39;n&#39;,
            &#39;/ľ/&#39; => &#39;l&#39;,
            &#39;/ý/&#39; => &#39;y&#39;,
            &#39;/ť/&#39; => &#39;t&#39;,
            &#39;/ž/&#39; => &#39;z&#39;,
            &#39;/š/&#39; => &#39;s&#39;,
            &#39;/æ/&#39; => &#39;ae&#39;,
            &#39;/ö/&#39; => &#39;oe&#39;,
            &#39;/ü/&#39; => &#39;ue&#39;,
            &#39;/Ä/&#39; => &#39;Ae&#39;,
            &#39;/Ü/&#39; => &#39;Ue&#39;,
            &#39;/Ö/&#39; => &#39;Oe&#39;,
            &#39;/ß/&#39; => &#39;ss&#39;,
            &#39;/ /&#39;=>&#39; &#39;,
            &#39;/ /&#39;=>&#39;&#39;,
            &#39;/~|·|!|@|#|¥|%|…|&|×|(|)|-|\+|=|『|【|』|】|、|:|;|“|”|\&#39;|《|,|》|。
            |?|\/|—|_|:|√|<|°|丶|>|-|★|||│|‖|ˇ/&#39;=>&#39; &#39;,
            &#39;/[^\w\s\x80-\xff]/&#39; => &#39; &#39;,
            &#39;/\\s+/&#39; => $replacement
        );
        $string = trim($string);
        $string = preg_replace(array_keys($map), array_values($map), $string);
        $string = preg_replace(&#39;/\\s+/&#39;,$replacement, strtolower($string));
        $string = trim($string,$replacement);
        return $string;
    }
     
    public function mktitle($string, $replacement = &#39;_&#39;)
    {
        $map = array(
            &#39;/à|á|å|â|ä/&#39; => &#39;a&#39;,
            &#39;/è|é|ê|ẽ|ë/&#39; => &#39;e&#39;,
            &#39;/ì|í|î/&#39; => &#39;i&#39;,
            &#39;/ò|ó|ô|ø/&#39; => &#39;o&#39;,
            &#39;/ù|ú|ů|û/&#39; => &#39;u&#39;,
            &#39;/ç|č/&#39; => &#39;c&#39;,
            &#39;/ñ|ň/&#39; => &#39;n&#39;,
            &#39;/ľ/&#39; => &#39;l&#39;,
            &#39;/ý/&#39; => &#39;y&#39;,
            &#39;/ť/&#39; => &#39;t&#39;,
            &#39;/ž/&#39; => &#39;z&#39;,
            &#39;/š/&#39; => &#39;s&#39;,
            &#39;/æ/&#39; => &#39;ae&#39;,
            &#39;/ö/&#39; => &#39;oe&#39;,
            &#39;/ü/&#39; => &#39;ue&#39;,
            &#39;/Ä/&#39; => &#39;Ae&#39;,
            &#39;/Ü/&#39; => &#39;Ue&#39;,
            &#39;/Ö/&#39; => &#39;Oe&#39;,
            &#39;/ß/&#39; => &#39;ss&#39;,
            &#39;/ /&#39;=>&#39; &#39;,
            &#39;/ /&#39;=>&#39;&#39;,
            &#39;/~|·|!|@|#|¥|%|…|&|×|(|)|-|\+|=|『|【|』|】|、|:|;|“|”|\&#39;|《|,|》|。
            |?|\/|—|_|:|√|<|°|丶|>|-|★|||│|‖|ˇ/&#39;=>&#39; &#39;,
            &#39;/[^\w\s^.\x80-\xff]/&#39; => &#39; &#39;,
            &#39;/\\s+/&#39; => $replacement
        );
        $string = trim($string);
        $string = preg_replace(array_keys($map), array_values($map), $string);
        $string = preg_replace(&#39;/\\s+/&#39;,$replacement, strtolower($string));
        $string = trim($string,$replacement);
        return $string;
    }
     
    // 全角替换成半角
    public function Qj2bj($string)
    {
        $qj2bj = array(
            &#39;1&#39; => &#39;1&#39;, &#39;2&#39; => &#39;2&#39;, &#39;3&#39; => &#39;3&#39;, &#39;4&#39; => &#39;4&#39;, &#39;5&#39; => &#39;5&#39;,
            &#39;6&#39; => &#39;6&#39;, &#39;7&#39; => &#39;7&#39;, &#39;8&#39; => &#39;8&#39;, &#39;9&#39; => &#39;9&#39;, &#39;0&#39; => &#39;0&#39;,
            &#39;a&#39; => &#39;a&#39;, &#39;b&#39; => &#39;b&#39;, &#39;c&#39; => &#39;c&#39;, &#39;d&#39; => &#39;d&#39;, &#39;e&#39; => &#39;e&#39;,
            &#39;f&#39; => &#39;f&#39;, &#39;g&#39; => &#39;g&#39;, &#39;h&#39; => &#39;h&#39;, &#39;i&#39; => &#39;i&#39;, &#39;j&#39; => &#39;j&#39;,
            &#39;k&#39; => &#39;k&#39;, &#39;l&#39; => &#39;l&#39;, &#39;m&#39; => &#39;m&#39;, &#39;n&#39; => &#39;n&#39;, &#39;o&#39; => &#39;o&#39;,
            &#39;p&#39; => &#39;p&#39;, &#39;q&#39; => &#39;q&#39;, &#39;r&#39; => &#39;r&#39;, &#39;s&#39; => &#39;s&#39;, &#39;t&#39; => &#39;t&#39;,
            &#39;u&#39; => &#39;u&#39;, &#39;v&#39; => &#39;v&#39;, &#39;w&#39; => &#39;w&#39;, &#39;x&#39; => &#39;x&#39;, &#39;y&#39; => &#39;y&#39;,
            &#39;z&#39; => &#39;z&#39;, &#39;A&#39; => &#39;A&#39;, &#39;B&#39; => &#39;B&#39;, &#39;C&#39; => &#39;C&#39;, &#39;D&#39; => &#39;D&#39;,
            &#39;E&#39; => &#39;E&#39;, &#39;F&#39; => &#39;F&#39;, &#39;G&#39; => &#39;G&#39;, &#39;H&#39; => &#39;H&#39;, &#39;I&#39; => &#39;I&#39;,
            &#39;J&#39; => &#39;J&#39;, &#39;K&#39; => &#39;K&#39;, &#39;L&#39; => &#39;L&#39;, &#39;M&#39; => &#39;M&#39;, &#39;N&#39; => &#39;N&#39;,
            &#39;O&#39; => &#39;O&#39;, &#39;P&#39; => &#39;P&#39;, &#39;Q&#39; => &#39;Q&#39;, &#39;R&#39; => &#39;R&#39;, &#39;S&#39; => &#39;S&#39;,
            &#39;T&#39; => &#39;T&#39;, &#39;U&#39; => &#39;U&#39;, &#39;V&#39; => &#39;V&#39;, &#39;W&#39; => &#39;W&#39;, &#39;X&#39; => &#39;X&#39;,
            &#39;Y&#39; => &#39;Y&#39;, &#39;Z&#39; => &#39;Z&#39;, &#39; &#39; => &#39; &#39;
        );
        return strtr($string, $qj2bj);
    }
     
    public function DelNbsp($string)
    {
        $map = array(
            &#39;/ /&#39;=>&#39; &#39;,
            &#39;/\?/&#39;=>&#39; &#39;,
            &#39;/ /&#39;=>&#39;&#39;,
        );
        $string = preg_replace(array_keys($map), array_values($map), $string);
        $string = preg_replace(&#39;/\\s+/&#39;,&#39;&#39;,$string);
        return $string;
    }
    public function getZipcode($str)
    {
        $pattern = "/[0-9]{1}(\d+){4,5}/";
        preg_match_all($pattern,$str,$zipcodeArr); 
        if(empty($zipcodeArr[0]))
        {
            return &#39;&#39;;
        }
        else {
            return $zipcodeArr[0][0];
        }
     
    }
 
    public function checkZipcode($str)
    {
        $strwidth=strlen($str);
        $zipcode=$str;
        switch($strwidth)
        {
            case 4:$zipcode="00".$str;break;
            case 5:$zipcode="0".$str;break;
            case 6:$zipcode=$str;break;
            default:$zipcode="";break;
        }
        return $zipcode;
    }
     
    public function getEmail($str) { 
        $pattern = "/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i"; 
        preg_match_all($pattern,$str,$emailArr); 
        if(empty($emailArr[0]))
        {
            return &#39;&#39;;
        }
        else {
            $email_str=implode(",", $emailArr[0]);
            return $email_str;
        }
    } 
    public function getUrl($str)
    {
        $pattern = "/(http:\/\/|https:\/\/|ftp:\/\/)?([\w:\/\.\?=&-_]+)/is"; 
        preg_match_all($pattern,$str,$urlArr);
        if(empty($urlArr[0]))
        {
            return &#39;&#39;;
        }
        else {
            $url_str=implode(",", $urlArr[0]);
            $url_str=preg_replace(&#39;/(http:\/\/|https:\/\/|ftp:\/\/)/&#39;,&#39;&#39;,$url_str);
            return $url_str;
        }
 
         
    }
    public function match_links($document) {  
    $match=array();  
       preg_match_all("&#39;<\s*a\s.*?href\s*=\s*([\"\&#39;])?(?(1)(.*?)\\1|([^\s\>]+))[^>]*>?(.*?)
       </a>&#39;isx",$document,$links);                        
       while(list($key,$val) = each($links[2])) {
           if(!empty($val))
               $match[&#39;link&#39;][] = $val;
       }
       while(list($key,$val) = each($links[3])) {
           if(!empty($val))
               $match[&#39;link&#39;][] = $val;
       }        
       while(list($key,$val) = each($links[4])) {
           if(!empty($val))
               $match[&#39;content&#39;][] = $val;
       }
       while(list($key,$val) = each($links[0])) {
           if(!empty($val))
               $match[&#39;all&#39;][] = $val;
       }                
       return $match;
    }
    public function DelNoStr($string)
    {
        $map = array(
            &#39;/à|á|å|â|ä/&#39; => &#39;a&#39;,
            &#39;/è|é|ê|ẽ|ë/&#39; => &#39;e&#39;,
            &#39;/ì|í|î/&#39; => &#39;i&#39;,
            &#39;/ò|ó|ô|ø/&#39; => &#39;o&#39;,
            &#39;/ù|ú|ů|û/&#39; => &#39;u&#39;,
            &#39;/ç|č/&#39; => &#39;c&#39;,
            &#39;/ñ|ň/&#39; => &#39;n&#39;,
            &#39;/ľ/&#39; => &#39;l&#39;,
            &#39;/ý/&#39; => &#39;y&#39;,
            &#39;/ť/&#39; => &#39;t&#39;,
            &#39;/ž/&#39; => &#39;z&#39;,
            &#39;/š/&#39; => &#39;s&#39;,
            &#39;/æ/&#39; => &#39;ae&#39;,
            &#39;/ö/&#39; => &#39;oe&#39;,
            &#39;/ü/&#39; => &#39;ue&#39;,
            &#39;/Ä/&#39; => &#39;Ae&#39;,
            &#39;/Ü/&#39; => &#39;Ue&#39;,
            &#39;/Ö/&#39; => &#39;Oe&#39;,
            &#39;/ß/&#39; => &#39;ss&#39;,
            &#39;/ /&#39;=>&#39; &#39;,
            &#39;/ /&#39;=>&#39;&#39;,
            &#39;/~|·|!|@|#|¥|%|…|&|×|(|)|-|\+|=|『|【|』|】|、|:|;|“|”|’|《|,|》|。
            |?|\/|—|_|‘|:|√|<|°|丶|ˇ/&#39;=>&#39; &#39;,
            &#39;/[^\w\s\x80-\xff]/&#39; => &#39; &#39;,
           // &#39;/\\s+/&#39; => $replacement
        );
        $string = trim($string);
        $string = preg_replace(array_keys($map), array_values($map), $string);
        $string = preg_replace(&#39;/\\s+/&#39;,&#39;&#39;,$string);
        $string = trim($string,&#39;&#39;);
        return $string;
    }
    public function UrlToStr($string, $find = &#39;/-/&#39;)
    {   
        $string = preg_replace($find," ", strtolower($string));
        $string = trim($string);
        return $string;
    }
     
    public function GetPyLetter($str)
    {
        return substr(pinyin($str, $ucfirst=true), 0,1);
    }
    public function GetPinYin($str)
    {
        return strtolower(pinyin($str, $ucfirst=true));
    }
     
    public function DelCode($str)
    {
         
        $search = array("&#39;<script[^>]*?>.*?</script>&#39;si",    // strip out javascript
                        "&#39;<[\/\!]*?[^<>]*?>&#39;si",            // strip out html tags
                        "&#39;([\r\n])[\s]+&#39;",                  // strip out white space
                        "&#39;&(quot|#34|#034|#x22);&#39;i",        // replace html entities
                        "&#39;&(amp|#38|#038|#x26);&#39;i",         // added hexadecimal values
                        "&#39;&(lt|#60|#060|#x3c);&#39;i",
                        "&#39;&(gt|#62|#062|#x3e);&#39;i",
                        "&#39;&(nbsp|#160|#xa0);&#39;i",
                        "&#39;&(iexcl|#161);&#39;i",
                        "&#39;&(cent|#162);&#39;i",
                        "&#39;&(pound|#163);&#39;i",
                        "&#39;&(copy|#169);&#39;i",
                        "&#39;&(reg|#174);&#39;i",
                        "&#39;&(deg|#176);&#39;i",
                        "&#39;&(#39|#039|#x27);&#39;",
                        "&#39;&(euro|#8364);&#39;i",                // europe
                        "&#39;&a(uml|UML);&#39;",                   // german
                        "&#39;&o(uml|UML);&#39;",
                        "&#39;&u(uml|UML);&#39;",
                        "&#39;&A(uml|UML);&#39;",
                        "&#39;&O(uml|UML);&#39;",
                        "&#39;&U(uml|UML);&#39;",
                        "&#39;&szlig;&#39;i",
                        );
        $replace = array("",
                            "",
                            "\\1",
                            "\"",
                            "&",
                            "<",
                            ">",
                            " ",
                            chr(161),
                            chr(162),
                            chr(163),
                            chr(169),
                            chr(174),
                            chr(176),
                            chr(39),
                            chr(128),
                            "?",
                            "?",
                            "?",
                            "?",
                            "?",
                            "?",
                            "?",
                        );
        $str = preg_replace($search,$replace,$str);
        return trim($str);
    }
     
     
    public function CutStr($sourcestr,$cutlength)
    {
       $returnstr=&#39;&#39;;
       $i=0;
       $n=0;
       $sourcestr=rtrim(SeoTools::DelCode($sourcestr));
       $str_length=strlen($sourcestr);//字符串的字节数
        while (($n<$cutlength) and ($i<=$str_length))
        {
          $temp_str=substr($sourcestr,$i,1);
          $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
          if ($ascnum>=224)    //如果ASCII位高与224,
          {
            $returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符       
             $i=$i+3;            //实际Byte计为3
             $n++;            //字串长度计1
          }
          elseif ($ascnum>=192) //如果ASCII位高与192,
          {
             $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
             $i=$i+2;            //实际Byte计为2
             $n++;            //字串长度计1
          }
          elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母,
          {
             $returnstr=$returnstr.substr($sourcestr,$i,1);
             $i=$i+1;            //实际的Byte数仍计1个
             $n++;            //但考虑整体美观,大写字母计成一个高位字符
          }
          else                //其他情况下,包括小写字母和半角标点符号,
          {
             $returnstr=$returnstr.substr($sourcestr,$i,1);
             $i=$i+1;            //实际的Byte数计1个
             $n=$n+0.5;        //小写字母和半角标点等与半个高位字符宽...
          }
        }
        if ($str_length>$i){
            $returnstr = $returnstr . ".";//超过长度时在尾处加上省略号
        }
        return htmlspecialchars($returnstr);
    }
     
    public function CreateTagLink($tags_str)
    {
        $tags_array=explode(",",$tags_str);
        $link_tpl="";
        foreach($tags_array as $v)
        {
            $tag=trim($v);
            $alias=SeoTools::friendlyURL($tag);
        //  $link_tpl.=&#39;<a href="/s/&#39;.urlencode($alias).&#39;" title="&#39;.$tag.&#39;">&#39;.$tag.&#39;</a>  &#39;;
            $link_tpl.=$tag.&#39;  &#39;;
        }
        return $link_tpl;
    }
     
}
 
 
 
 
/**
 * 中文转拼类
 *
 * @author  Lukin <my@lukin.cn>
 * @date    2011-01-25 10:44
 */
class PinYin {
    // 码表
    private $fp  = null;
    private $dat = &#39;pinyin.dat&#39;;
 
    public function __construct(){
        $this->dat = dirname(__FILE__).&#39;/&#39;.$this->dat;
        if (is_file($this->dat)) {
            $this->fp = fopen($this->dat, &#39;rb&#39;);
        }
    }
    /**
     * 转拼音
     *
     * @param string $str   汉字
     * @param bool $ucfirst 首字母大写
     * @param bool $polyphony 忽略多读音
     * @return string
     */
    public function encode($str, $ucfirst=true, $polyphony=true) {
        $ret = &#39;&#39;; $len = mb_strlen($str, &#39;UTF-8&#39;);
        for ($i = 0; $i < $len; $i++) {
            $py = $this->pinyin(mb_substr($str, $i, 1, &#39;UTF-8&#39;));
            if ($ucfirst && strpos($py,&#39;,&#39;) !== false) {
                $pys = explode(&#39;,&#39;, $py); 
                $ret.= implode(&#39;,&#39;, array_map(&#39;ucfirst&#39;, ($polyphony ? array_slice($pys, 0, 1) : $pys)));
            } else {
                $ret.= $ucfirst ? ucfirst($py) : $py;
               
            }
        }
         
        return $ret;
    }
    /**
     * 汉字转十进制
     *
     * @param string $word
     * @return number
     */
    private function char2dec($word) {
        $bins  = &#39;&#39;;
        $chars = str_split($word);
        foreach($chars as $char) $bins.= decbin(ord($char));
        $bins = preg_replace(&#39;/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/&#39;, &#39;$1$2$3&#39;, $bins);
        return bindec($bins);
    }
    /**
     * 单个字转拼音
     *
     * @param string $char  汉字
     * @return string
     */
    public function pinyin($char){
        if (strlen($char) == 3 && $this->fp) {
            $offset = $this->char2dec($char);
            // 判断 off 值
            if ($offset >= 0) {
                fseek($this->fp, ($offset - 19968) << 4, SEEK_SET);
                return trim(fread($this->fp, 16));
            }
        }
        return $char;
    }
 
    public function __destruct() {
        if ($this->fp) {
            fclose($this->fp);
        }
    }
}
/**
 * 取得实例
 *
 * @return $pinyin
 */
function &_pinyin_get_object() {
    static $pinyin;
    if ( is_null($pinyin) )
        $pinyin = new PinYin();
    return $pinyin;
}
 
if (!function_exists(&#39;pinyin&#39;)) :
/**
 * 取得拼音
 *
 * @param string $str
 * @param bool $ucfirst 首字母大写
 * @return string
 */
function pinyin($str, $ucfirst=true) {
    $py = _pinyin_get_object();
    return $py->encode($str, $ucfirst);
}
endif;
Copy after login

以上就是分享一个PHP 网站SEO及过滤类,方便使用的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!