Home > Article > Backend Development > Chinese and English mixed string interception
<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); }); });
The above introduces Chinese and English mixed string interception, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.