溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用PHP切割漢字

發(fā)布時(shí)間:2021-05-18 17:08:01 來(lái)源:億速云 閱讀:115 作者:Leah 欄目:開發(fā)技術(shù)

如何使用PHP切割漢字?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

具體如下:

<?php
/*
@UTF-8編碼的字符可能由1~3個(gè)字節(jié)組成。
*/
/*--------------------------方法一截取中文字符串方法------------------------------*/
function msubstr($str, $start, $len)
{
  $tmpstr = "";
  $strlen = $start + $len;
  for ($i = 0; $i < $strlen; $i++) {
    if (ord(substr($str, $i, 1)) > 0xa0)  //ord()函數(shù)返回字符串的第一個(gè)字符的ASCII值
    {
      $tmpstr .= substr($str, $i, 2);
      $i++;
    } else {
      $tmpstr .= substr($str, $i, 1);
    }
  }
  return $tmpstr;
}
/*----------------------------第二種方法-----------------------------------*/
//截取的是UTF-8字符串
function utf_substr($str, $len)
{
  $new_str = [];
  for ($i = 0; $i < $len; $i++) {
    $tem_str = substr($str, 0, 1);
    if (ord($tem_str > 127)) {
      $i++;
      if ($i < $len) {
        $new_str[] = substr($str, 0, 3);
        $str = substr($str, 3);
      }
    } else {
      $new_str[] = substr($str, 0, 1);
      $str = substr($str, 1);
    }
  }
  return join($new_str);//join()函數(shù)把數(shù)組元素組合為一個(gè)字符串
}
/*-------------------------------------第三種方法(UTF-8)--------------------------------*/
function cutstr($string, $length)
{
  preg_match_all("/[\x01-\x7f]|[\xc2-\xdf]|[\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
  $wordscut = "";
  $j = 0;
  for ($i = 0; $i < count($info[0]); $i++) {
    $wordscut .= $info[0][$i];
    $j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
    if ($j > $length - 3) {
      return $wordscut . "...";
    }
  }
  return join('', $info[0]);
}
$string = "312哈哈,這個(gè)組合很難切割哦";
echo cutstr($string, 10);
/*---------------------------------下面是曾經(jīng)用過(guò)的截取第三個(gè)的字符串的------------------------------*/
// $name1 = mysql_result($my_rst,0,"name");
// $name = preg_match("/([1-9][0-9]+)/",$name1,$r);
// $name = $r[0];
// if($name == ""){
// $name=preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,2}'.
// '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,1}).*#s',
// '$1',$name1);
// }
/*--------------------------------------------第四種方法(UTF-8)---------------------------------------------*/
function cut_str($sourcestr, $cutlength)
{
  $returnstr = '';
  $i = 0;
  $n = 0;
  $str_length = strlen($sourcestr);//字符串的字節(jié)數(shù)
  while ($n < $cutlength && $i <= $str_length) {
    $temp_str = substr($sourcestr, $i, 1);
    $ascnum = ord($temp_str);//得到字符串中第$i位字符的ASCII碼
    if ($ascnum >= 224) {
      $returnstr = $returnstr . substr($sourcestr, $i, 3);//根據(jù)UTF-8編碼規(guī)范,將3個(gè)連續(xù)的字符計(jì)為單個(gè)字符
      $i = $i + 3;//實(shí)際Byte記為3
      $n++;//字串長(zhǎng)度為1
    } elseif ($ascnum >= 192)//如果ASCII位高于192
    {
      $returnstr = $returnstr . substr($sourcestr, $i, 2);//根據(jù)UTF-8編碼規(guī)范,將2個(gè)連續(xù)的字符記為單個(gè)字符
      $i = $i + 2;//實(shí)際Byte記為2
      $n++;//字串長(zhǎng)度為1
    } elseif ($ascnum >= 65 && $ascnum <= 90)//如果是大寫字母
    {
      $returnstr = $returnstr . substr($sourcestr, $i, 1);
      $i = $i + 1;//byte記為1
      $n++;//但考慮到整體美觀,大寫字母計(jì)成一個(gè)高位字符
    } else {
      $returnstr = $returnstr . substr($sourcestr, $i, 1);
      $i = $i + 1;//實(shí)際的Byte記為1
      $n = $n + 0.5;//小寫字母和半角標(biāo)點(diǎn)等與半個(gè)高位字符寬...
    }
  }
  if ($str_length > $cutlength) {
    $returnstr = $returnstr . "...";//超過(guò)長(zhǎng)度時(shí)在尾處加上省略號(hào)
  }
  return $returnstr;
}
/*--------------------第五種方法(UTF-8)---------------------------------------------*/
function FSubstr($title, $start, $len = "", $magic = true)
{
  if ($len == "") $len = strlen($title);
  if ($start != 0) {
    $startv = ord(substr($title, $start, 1));
    if ($startv >= 128) {
      if ($startv < 192) {
        for ($i = $start - 1; $i > 0; $i--) {
          $tempv = ord(substr($title, $i, 1));
          if ($tempv >= 192) break;
        }
        $start = $i;
      }
    }
  }
  if (strlen($title) <= $len) return substr($title, $start, $len);
  $alen = 0;
  $blen = 0;
  $realnum = 0;
  $length = 0;
  for ($i = $start; $i < strlen($title); $i++) {
    $ctype = 0;
    $cstep = 0;
    $cur = substr($title, $i, 1);
    if ($cur == "&") {
      if (substr($title, $i, 4) == "&lt;") {
        $cstep = 4;
        $length += 4;
        $i += 3;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (substr($title, $i, 4) == "&gt;") {
        $cstep = 4;
        $length += 4;
        $i += 3;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (substr($title, $i, 5) == "&amp;") {
        $cstep = 5;
        $length += 5;
        $i += 4;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (substr($title, $i, 6) == "&quot;") {
        $cstep = 6;
        $length += 6;
        $i += 5;
        $realnum++;
        if ($magic) {
          $alen++;
        }
      } elseif (preg_match("/&#(\d+);?/i", substr($title, $i), $match)) {
        $cstep = strlen($match[0]);
        $length += strlen($match[0]);
        $i += strlen($match[0]) - 1;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      }
    } else {
      if (ord($cur) >= 252) {
        $cstep = 6;
        $length += 6;
        $i += 5;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      } elseif (ord($cur) >= 248) {
        $cstep = 5;
        $length += 5;
        $i += 4;
        $realnum++;
        if ($magic) {
          $ctype = 1;
          $blen++;
        }
      } elseif (ord($cur) >= 240) {
        $cstep = 4;
        $length += 4;
        $i += 3;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      } elseif (ord($cur) >= 224) {
        $cstep = 3;
        $length += 3;
        $i += 2;
        $realnum++;
        if ($magic) {
          $ctype = 1;
          $blen++;
        }
      } elseif (ord($cur) >= 192) {
        $ctype = 2;
        $length += 2;
        $i += 1;
        $realnum++;
        if ($magic) {
          $blen++;
          $ctype = 1;
        }
      } elseif (ord($cur) >= 128) {
        $length += 1;
      } else {
        $cstep = 1;
        $length += 1;
        $realnum++;
        if ($magic) {
          if (ord($cur) >= 65 && ord($cur) <= 90) {
            $blen++;
          } else {
            $alen++;
          }
        }
      }
    }
    if ($magic) {
      if (($blen * 2 + $alen) == ($len * 2)) break;
      if (($blen * 2 + $alen) == ($len * 2) + 1) {
        if ($ctype == 1) {
          $length -= $cstep;
          break;
        } else {
          break;
        }
      }
    } else {
      if ($realnum == $len) break;
    }
  }
  unset($cur);
  unset($alen);
  unset($blen);
  unset($realnum);
  unset($ctype);
  unset($cstep);
  return substr($title, $start, $length);
}
function utf8Substr($str, $from, $len)
{
  return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' .
    '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s',
    '$1', $str);
}
$title = "你哈珀niad1納斯達(dá)wop asdni你愛誰(shuí)都沒(méi)阿斯頓撒旦12ccs- sd";
$title = utf8Substr($title, 0, 15);
echo $title;
?>

php的框架有哪些

php的框架:1、Laravel,Laravel是一款免費(fèi)并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運(yùn)行速度最快的一個(gè)PHP框架。3、Symfony,Symfony是一款為Web項(xiàng)目準(zhǔn)備的PHP框架。4、Yii,Yii是一款快速、安全和專業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強(qiáng)大的PHP框架。

關(guān)于如何使用PHP切割漢字問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI