Joomla framework string interception example

小云云
Release: 2023-03-19 22:10:02
Original
1223 people have browsed it

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,&#39;UTF8&#39;)<= $number){//&#39;UTF8&#39;跟据字符串的格式调整
    return $str;
  }
  else{
    if($en)
    {
      if($cn){
        //中英文混合情况下
        return mb_substr($str,0,$number+2,&#39;utf-8&#39;).&#39;...&#39;;
      }
      else{
        //全为英文情况下
        return mb_substr($str,0,$number+4,&#39;utf-8&#39;).&#39;...&#39;;
      }
    }
    else {
      //全为中文情况下
      return mb_substr($str,0,$number,&#39;utf-8&#39;).&#39;...&#39;;
    }
  }
}
Copy after login

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!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template