Home > php教程 > php手册 > body text

分享一个多语言截取字符串功能

WBOY
Release: 2016-06-07 11:38:58
Original
1120 people have browsed it

在自己项目用到的哈,英文和中文往往字数不一样。用到多语言的项目记得收藏哦!~完全原创~
function msubstr($str, $start = 0, $length, $lenth2, $suffix = true) {<br> //$length 中文截取长度,$lenth2英文截取长度 $suffix 是否省略号<br>     $charset = 'utf-8';<br>     if (LANG_SET != 'zh') {<br>         $length = $lenth2;<br>     }<br>     $str = preg_replace("/(\|\r|\n|\s|\[.+?\])/is", ' ', $str);<br>     if (function_exists("mb_substr"))<br>         $slice = mb_substr($str, $start, $length, $charset);<br>     elseif (function_exists('iconv_substr')) {<br>         $slice = iconv_substr($str, $start, $length, $charset);<br>         if (false === $slice) {<br>             $slice = '';<br>         }<br>     } else {<br>         $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";<br>         $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";<br>         $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";<br>         $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";<br>         preg_match_all($re[$charset], $str, $match);<br>         $slice = join("", array_slice($match[0], $start, $length));<br>     }<br>     $fix = '';<br>     if (LANG_SET == 'zh') {<br>         $slice = str_replace(' ', '', $slice);<br>         if (strlen($slice) > $length) {<br>             $fix = '...';<br>         }<br>     } else {<br>         if (strlen($str) > $lenth2) {<br>             $fix = '...';<br>         }<br>     }<br> <br>     return $suffix ? $slice . $fix : $slice;<br> }本文转自素材火 http://www.sucaihuo.com
有bug的联系我,赶紧纠正哈。。

AD:真正免费,域名+虚机+企业邮箱=0元

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!