日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

php:根據(jù)中文裁減字符串函數(shù)方法

系統(tǒng) 2272 0
        define(CHARSET, 'UTF-8'); // 系統(tǒng)默認編碼



    /**

     * 根據(jù)中文裁減字符串

     * @param $string - 字符串

     * @param $length - 長度

     * @param $doc - 縮略后綴

     * @return 返回帶省略號被裁減好的字符串

     */

    function cutstr( $string, $length, $dot = '...' ) {

        if ( strlen( $string ) <= $length ) {

            return $string;

        }

        $pre = chr( 1 );

        $end = chr( 1 );

        $string = str_replace( array( '&', '"', '<', '>' ), array( $pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end ), $string );

        $strcut = '';

        if ( strtolower( CHARSET ) == 'utf-8' ) {

            $n = $tn = $noc = 0;

            while ( $n < strlen( $string ) ) {

                $t = ord( $string[$n] );

                if ( $t == 9 || $t == 10 || (32 <= $t && $t <= 126) ) {

                    $tn = 1;

                    $n++;

                    $noc++;

                } elseif ( 194 <= $t && $t <= 223 ) {

                    $tn = 2;

                    $n += 2;

                    $noc += 2;

                } elseif ( 224 <= $t && $t <= 239 ) {

                    $tn = 3;

                    $n += 3;

                    $noc += 2;

                } elseif ( 240 <= $t && $t <= 247 ) {

                    $tn = 4;

                    $n += 4;

                    $noc += 2;

                } elseif ( 248 <= $t && $t <= 251 ) {

                    $tn = 5;

                    $n += 5;

                    $noc += 2;

                } elseif ( $t == 252 || $t == 253 ) {

                    $tn = 6;

                    $n += 6;

                    $noc += 2;

                } else {

                    $n++;

                }

                if ( $noc >= $length ) {

                    break;

                }

            }

            if ( $noc > $length ) {

                $n -= $tn;

            }

            $strcut = substr( $string, 0, $n );

        } else {

            for ( $i = 0; $i < $length; $i++ ) {

                $strcut .= ord( $string[$i] ) > 127 ? $string[$i] . $string[++$i] : $string[$i];

            }

        }

        $strcut = str_replace( array( $pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end ), array( '&', '"', '<', '>' ), $strcut );

        $pos = strrpos( $strcut, chr( 1 ) );

        if ( $pos !== false ) {

            $strcut = substr( $strcut, 0, $pos );

        }

        return $strcut . $dot;

    }
  


php:根據(jù)中文裁減字符串函數(shù)方法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 城口县| 肥西县| 民丰县| 和田县| 田东县| 桓仁| 海林市| 汝阳县| 巴中市| 区。| 邻水| 霍山县| 仁寿县| 三门县| 钟山县| 高碑店市| 盐亭县| 游戏| 庐江县| 洛隆县| 吴桥县| 宿松县| 永顺县| 黎平县| 视频| 佳木斯市| 上饶县| 漯河市| 兖州市| 新丰县| 邵阳市| 九江市| 嘉祥县| 临江市| 邛崃市| 调兵山市| 丰顺县| 建德市| 西乌珠穆沁旗| 越西县| 阿尔山市|