Home >Backend Development >PHP Tutorial >Introduction to the method of intercepting string implementation in Joomla framework

Introduction to the method of intercepting string implementation in Joomla framework

巴扎黑
巴扎黑Original
2017-08-14 09:55:291188browse

This article mainly introduces the method of Joomla framework to implement string interception, involving PHP string and regular operation related skills. Friends in need can refer to it

The example of this article tells the Joomla framework to implement string interception Methods. Share it with everyone for your reference, the details are as follows:

When developing with joomla, you need to use foreign resources, some modules, components, plug-ins and the like, but we will find that in the string All methods need 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.

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;;
    }
  }
}

Readers who are interested in more PHP framework related content can check out the special topics of this site: "Summary of Excellent PHP Development Framework", "ThinkPHP Introductory Tutorial", "codeigniter Introductory Tutorial" ", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introductory Tutorial", "Smarty Template Basic Tutorial" and "PHP Template Technology Summary".

The above is the detailed content of Introduction to the method of intercepting string implementation in Joomla framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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