• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用php实现截取指定长度_php技巧

php 搞代码 4年前 (2022-01-26) 30次浏览 已收录 0个评论

PHP语言: 根据中文裁减字符串

function cutstr($string,$length, $dot = ' ...') {<BR>  if (strlen($string) <= $length) {<BR>      return$string;<BR>  }<BR>  $pre = chr(1);<BR>  $end = chr(1);<BR>  $string = str_replace(array('&','"', ''),array($pre . '&'. $end, $pre . '"' . $end, $pre . '' . $end), $string);<br><br>  $strcut = '';<BR>  if (strtolower(CHARSET) == 'utf-8'){<BR>      $n = $tn =$noc = 0;<BR>      while ($n< strlen($string)) {<br><br>         $t =ord($string[$n]);<BR>         if ($t == 9 || $t == 10 || (32<= $t && $t <= 126)) {<BR>            $tn = 1;<BR>      <a style="color:transparent">本@文来源gao($daima.com搞@代@#码(网5</a><strong>搞gaodaima代码</strong>      $n++;<BR>            $noc++;<BR>         } elseif (194 <= $t&& $t <= 223) {<BR>            $tn = 2;<BR>            $n += 2;<BR>            $noc += 2;<BR>         } elseif (224 <= $t&& $t <= 239) {<BR>            $tn = 3;<BR>            $n += 3;<BR>            $noc += 2;<BR>         } elseif (240 <= $t&& $t <= 247) {<BR>            $tn = 4;<BR>            $n += 4;<BR>            $noc += 2;<BR>         } elseif (248 <= $t&& $t <= 251) {<BR>            $tn = 5;<BR>            $n += 5;<BR>            $noc += 2;<BR>         } elseif ($t == 252 || $t ==253) {<BR>            $tn = 6;<BR>            $n += 6;<BR>            $noc += 2;<BR>         } else {<BR>            $n++;<BR>         }<br><br>         if ($noc >= $length){<BR>            break;<BR>         }<BR>     }<BR>      if ($noc> $length) {<BR>         $n -= $tn;<BR>     }<BR>      $strcut =substr($string, 0, $n);<BR>  } else {<BR>      for ($i =0; $i < $length; $i++) {<BR>         $strcut .= ord($string[$i])> 127 ? $string[$i] . $string[++$i] : $string[$i];<BR>     }<BR>  }<BR>  $strcut = str_replace(array($pre . '&' .$end, $pre . '"' . $end, $pre . '' .$end), array('&', '"',''), $strcut);<br><br>  $pos = strrpos($strcut, chr(1));<BR>  if ($pos !== false) {<BR>      $strcut =substr($strcut, 0, $pos);<BR>  }<BR>  return $strcut . $dot;<BR>}<BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用php实现截取指定长度_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址