Home  >  Article  >  php教程  >  用于截取特定长度的字符串的php函数

用于截取特定长度的字符串的php函数

WBOY
WBOYOriginal
2016-06-13 10:32:06944browse

* 用于截取特定长度的字符串的php(做为现在的主流开发语言)函数,同时处理中文字符.
* 返回截取之后的字符串;若字符串长度小于或等于参数 $str_len 则返回原字符串。
*
* @access(小型网站之最爱)  public
* @param   string  $str        需要截取的字符串。
* @param   integer $str_len    截取的字符串长度。
* @param   string  $str_append 截取操作发生时,在被截取字符串最后边增加的字符串,默认值是


  function my_substr($str, $str_len, $str_append =   ... ...)
  {
      if (strlen($str) > $str_len) {
          $str_len -= strlen($str_append);
          for ($i = $str_len, $j = 0; $i >= 0, ord($str[$i - 1]) > 128; $i--, $j++);;
          $str = ($j % 2 != 0)
              ? substr($str, 0, $str_len - 1).
              : substr($str, 0, $str_len);
          $str .= $str_append;
      }   //end if

      return $str;
  }   //end function    

?>
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