首页  >  文章  >  后端开发  >  中英文混合字符串截取

中英文混合字符串截取

WBOY
WBOY原创
2016-07-29 09:04:25977浏览

<code><span>/**
    *   功能:中英文混合字符串截取(指定开始位置,截取长度,一个汉字相对于一个字符)
    *   参数:传入一个字符串
    *   返回值: 字符串
    */</span><span><span>function</span><span>cc_msubstr</span><span>(<span>$str</span>, <span>$length</span>, <span>$start</span>=<span>0</span>, <span>$charset</span>=<span>"utf-8"</span>, <span>$suffix</span>=true)</span>{</span><span>if</span>(function_exists(<span>"mb_substr"</span>)){
            <span>return</span> mb_substr(<span>$str</span>, <span>$start</span>, <span>$length</span>, <span>$charset</span>);
        }<span>elseif</span>(function_exists(<span>'iconv_substr'</span>)){
            <span>return</span> iconv_substr(<span>$str</span>,<span>$start</span>,<span>$length</span>,<span>$charset</span>);
        }
        <span>$re</span>[<span>'utf-8'</span>]  = <span>"/[/x01-/x7f]|[/xc2-/xdf][/x80-/xbf]|[/xe0-/xef][/x80-/xbf]{2}|[/xf0-/xff][/x80-/xbf]{3}/"</span>;
        <span>$re</span>[<span>'gb2312'</span>] = <span>"/[/x01-/x7f]|[/xb0-/xf7][/xa0-/xfe]/"</span>;
        <span>$re</span>[<span>'gbk'</span>]    = <span>"/[/x01-/x7f]|[/x81-/xfe][/x40-/xfe]/"</span>;
        <span>$re</span>[<span>'big5'</span>]   = <span>"/[/x01-/x7f]|[/x81-/xfe]([/x40-/x7e]|/xa1-/xfe])/"</span>;
        preg_match_all(<span>$re</span>[<span>$charset</span>], <span>$str</span>, <span>$match</span>);
        <span>$slice</span> = join(<span>""</span>,array_slice(<span>$match</span>[<span>0</span>], <span>$start</span>, <span>$length</span>));
        <span>if</span>(<span>$suffix</span>){
            <span>return</span><span>$slice</span>.<span>".."</span>;
        }<span>else</span>{
            <span>return</span><span>$slice</span>;
        }
   }</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了中英文混合字符串截取,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn