欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP切割漢字的常用方法實例總結

 更新時間:2019年04月27日 11:33:01   作者:雨落知音  
這篇文章主要介紹了PHP切割漢字的常用方法,結合實例形式總結分析了php針對漢字的編碼轉換、遍歷、截取等相關操作技巧,需要的朋友可以參考下

本文實例講述了PHP切割漢字的常用方法。分享給大家供大家參考,具體如下:

<?php
/*
@UTF-8編碼的字符可能由1~3個字節(jié)組成。
*/
/*--------------------------方法一截取中文字符串方法------------------------------*/
function msubstr($str, $start, $len)
{
  $tmpstr = "";
  $strlen = $start + $len;
  for ($i = 0; $i < $strlen; $i++) {
    if (ord(substr($str, $i, 1)) > 0xa0)  //ord()函數返回字符串的第一個字符的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()函數把數組元素組合為一個字符串
}
/*-------------------------------------第三種方法(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哈哈,這個組合很難切割哦";
echo cutstr($string, 10);
/*---------------------------------下面是曾經用過的截取第三個的字符串的------------------------------*/
// $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é)數
  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);//根據UTF-8編碼規(guī)范,將3個連續(xù)的字符計為單個字符
      $i = $i + 3;//實際Byte記為3
      $n++;//字串長度為1
    } elseif ($ascnum >= 192)//如果ASCII位高于192
    {
      $returnstr = $returnstr . substr($sourcestr, $i, 2);//根據UTF-8編碼規(guī)范,將2個連續(xù)的字符記為單個字符
      $i = $i + 2;//實際Byte記為2
      $n++;//字串長度為1
    } elseif ($ascnum >= 65 && $ascnum <= 90)//如果是大寫字母
    {
      $returnstr = $returnstr . substr($sourcestr, $i, 1);
      $i = $i + 1;//byte記為1
      $n++;//但考慮到整體美觀,大寫字母計成一個高位字符
    } else {
      $returnstr = $returnstr . substr($sourcestr, $i, 1);
      $i = $i + 1;//實際的Byte記為1
      $n = $n + 0.5;//小寫字母和半角標點等與半個高位字符寬...
    }
  }
  if ($str_length > $cutlength) {
    $returnstr = $returnstr . "...";//超過長度時在尾處加上省略號
  }
  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納斯達wop asdni你愛誰都沒阿斯頓撒旦12ccs- sd";
$title = utf8Substr($title, 0, 15);
echo $title;
?>

更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP編碼與轉碼操作技巧匯總》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php常用函數與技巧總結》及《PHP錯誤與異常處理方法總結

希望本文所述對大家PHP程序設計有所幫助。

相關文章

  • 淺析php如何實現爬取數據原理

    淺析php如何實現爬取數據原理

    在本篇文章中,小編給大家分享了關于php如何實現爬取數據的原理知識點,有興趣的朋友們參考下。
    2018-09-09
  • 使用PHP生成PDF方法詳解

    使用PHP生成PDF方法詳解

    這篇文章主要介紹了使用PHP生成PDF方法詳解的相關資料,需要的朋友可以參考下
    2015-01-01
  • php調用mysql數據 dbclass類

    php調用mysql數據 dbclass類

    php調用mysql數據數據庫操作類,dbclass類,需要的朋友可以參考下。
    2011-05-05
  • php防攻擊代碼升級版

    php防攻擊代碼升級版

    我上一篇文章《最新開發(fā)的網站防IP攻擊代碼,超級有用》寫了一個完整的防止網絡惡意IP攻擊的方案,使用了一個月,效果良好。
    2010-12-12
  • php中判斷字符串是否全是中文或含有中文的實現代碼

    php中判斷字符串是否全是中文或含有中文的實現代碼

    php中判斷字符串是否全是中文或含有中文的實現代碼,主要是利用正則匹配中文判定。
    2011-09-09
  • Ubuntu中支持PHP5與PHP7雙版本的簡單實現

    Ubuntu中支持PHP5與PHP7雙版本的簡單實現

    這篇文章主要給大家介紹了關于Ubuntu中支持PHP5與PHP7雙版本的簡單實現方法,文中通過示例代碼介紹的非常詳細,這個方法也非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧
    2018-08-08
  • php字符串函數學習之substr()

    php字符串函數學習之substr()

    這篇文章主要介紹了php字符串函數學習之substr(),本文講解了它的定義和用法、參數描述、提示和注釋以及多個使用示例 ,需要的朋友可以參考下
    2015-03-03
  • 怎樣使用php與jquery設置和讀取cookies

    怎樣使用php與jquery設置和讀取cookies

    有兩種處理cookies的方式—服務端(php,asp等)和客戶端(javascript).在這個教程中,我們將學習到以php和javascript這兩種方式如何去創(chuàng)建cookies
    2013-08-08
  • PHP數字金額轉換成中文大寫顯示

    PHP數字金額轉換成中文大寫顯示

    今天小編就為大家分享一篇關于PHP數字金額轉換成中文大寫顯示,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • PHP使用ajax的post方式下載excel文件簡單示例

    PHP使用ajax的post方式下載excel文件簡單示例

    這篇文章主要介紹了PHP使用ajax的post方式下載excel文件,結合簡單示例形式分析了php后端Excel文件下載及前端ajax交互相關操作技巧,需要的朋友可以參考下
    2019-08-08

最新評論