Home > Backend Development > PHP Tutorial > php:依据中文裁减字符串函数方法

php:依据中文裁减字符串函数方法

WBOY
Release: 2016-06-13 12:37:45
Original
843 people have browsed it

php:根据中文裁减字符串函数方法

    define(CHARSET, 'UTF-8'); // 系统默认编码

    /**
     * 根据中文裁减字符串
     * @param $string - 字符串
     * @param $length - 长度
     * @param $doc - 缩略后缀
     * @return 返回带省略号被裁减好的字符串
     */
    function cutstr( $string, $length, $dot = '...' ) {
        if ( strlen( $string ) ' ), array( $pre . '&' . $end, $pre . '"' . $end, $pre . '' . $end ), $string );
        $strcut = '';
        if ( strtolower( CHARSET ) == 'utf-8' ) {
            $n = $tn = $noc = 0;
            while ( $n = $length ) {
                    break;
                }
            }
            if ( $noc > $length ) {
                $n -= $tn;
            }
            $strcut = substr( $string, 0, $n );
        } else {
            for ( $i = 0; $i  127 ? $string[$i] . $string[++$i] : $string[$i];
            }
        }
        $strcut = str_replace( array( $pre . '&' . $end, $pre . '"' . $end, $pre . '' . $end ), array( '&', '"', '' ), $strcut );
        $pos = strrpos( $strcut, chr( 1 ) );
        if ( $pos !== false ) {
            $strcut = substr( $strcut, 0, $pos );
        }
        return $strcut . $dot;
    }
Copy after login

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template