When developing with joomla, we need to use foreign resources, some modules, components, plug-ins, etc., but we will find that the string method needs to be modified. Because PHP's substr method is only effective for non-Chinese strings, another simple and easy method mb_substr should be used, which can easily solve the problem of intercepting characters.
This article mainly introduces the Joomla framework to implement string interception, involving techniques related to PHP strings and regular operations. Friends in need can refer to it. I hope it can help you.
At the same time, if you need to intercept strings in three ways (except punctuation marks) in Chinese, English, and Chinese and English mixed arrangements, then regular expressions will come in handy. The source code is attached for reference only.
/*截取字符串方法*/ //$str字符串 //$number为最大长度 function cutStrTitle($str, $number){ $str = strip_tags($str); $en=preg_match('/^[a-zA-Z]/', $str);//匹配英文字母 $cn=preg_match_all("/([\x{4e00}-\x{9fa5}]){1}/u",$str,$arrc);//匹配汉字,统计个数,返回给$arrc if(mb_strlen($str,'UTF8')<= $number){//'UTF8'跟据字符串的格式调整 return $str; } else{ if($en) { if($cn){ //中英文混合情况下 return mb_substr($str,0,$number+2,'utf-8').'...'; } else{ //全为英文情况下 return mb_substr($str,0,$number+4,'utf-8').'...'; } } else { //全为中文情况下 return mb_substr($str,0,$number,'utf-8').'...'; } } }
Related recommendations:
php Chinese string interception method example summary
The above is the detailed content of Joomla framework string interception example. For more information, please follow other related articles on the PHP Chinese website!