Rumah > pembangunan bahagian belakang > tutorial php > 中英文混合字符串截取

中英文混合字符串截取

WBOY
Lepaskan: 2016-07-29 09:04:25
asal
1005 orang telah melayarinya

<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>
Salin selepas log masuk
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan