茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    PHP截取中文字符串方法總結(jié)
    來源:易賢網(wǎng) 閱讀:2793 次 日期:2014-08-21 17:00:14
    溫馨提示:易賢網(wǎng)小編為您整理了“PHP截取中文字符串方法總結(jié)”,方便廣大網(wǎng)友查閱!

    程序一:PHP截取中文字符串方法

    由于網(wǎng)站首頁以及vTigerCRM里經(jīng)常在截取中文字符串時出現(xiàn)亂碼(使用substr),今天找到一個比較好的截取中文字符串方法,在此與大家共享。

    function msubstr($str, $start, $len) {

    $tmpstr = "";

    $strlen = $start + $len;

    for($i = 0; $i < $strlen; $i++) {

    if(ord(substr($str, $i, 1)) > 0xa0) {

    $tmpstr .= substr($str, $i, 2);

    $i++;

    } else

    $tmpstr .= substr($str, $i, 1);

    }

    return $tmpstr;

    }

    程序二:PHP截取UTF-8字符串,解決半字符問題

    /******************************************************************

    * PHP截取UTF-8字符串,解決半字符問題。

    * 英文、數(shù)字(半角)為1字節(jié)(8位),中文(全角)為3字節(jié)

    * @return 取出的字符串, 當(dāng)$len小于等于0時, 會返回整個字符串

    * @param $str 源字符串

    * $len 左邊的子串的長度

    ****************************************************************/

    function utf_substr($str,$len)

    {

    for($i=0;$i<$len;$i++)

    {

    $temp_str=substr($str,0,1);

    if(ord($temp_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);

    }

    ?>

    php 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);

    for($i=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="242432反對感是456犯得上廣泛大使館地方7890";

    for($i=0;$i

    {

    echo cutstr($string,$i)."

    ";

    }

    ?>

    截取utf-8字符串函數(shù)

    為了支持多語言,數(shù)據(jù)庫里的字符串可能保存為UTF-8編碼,在網(wǎng)站開發(fā)中可能需要用php截取字符串的一部分。為了避免出現(xiàn)亂碼現(xiàn)象,編寫如下的UTF-8字符串截取函數(shù)

    關(guān)于utf-8的原理請看 UTF-8 FAQ

    UTF-8編碼的字符可能由1~3個字節(jié)組成, 具體數(shù)目可以由第一個字節(jié)判斷出來。(理論上可能更長,但這里假設(shè)不超過3個字節(jié))

    第一個字節(jié)大于224的,它與它之后的2個字節(jié)一起組成一個UTF-8字符

    第一個字節(jié)大于192小于224的,它與它之后的1個字節(jié)組成一個UTF-8字符

    否則第一個字節(jié)本身就是一個英文字符(包括數(shù)字和一小部分標(biāo)點符號)。

    以前為某網(wǎng)站設(shè)計的代碼(也是現(xiàn)在用在首頁的長度截取的函數(shù))

    //$sourcestr 是要處理的字符串

    //$cutlength 為截取的長度(即字?jǐn)?shù))

    function cut_str($sourcestr,$cutlength)

    {

    $returnstr='';

    $i=0;

    $n=0;

    $str_length=strlen($sourcestr);//字符串的字節(jié)數(shù)

    while (($n<$cutlength) and ($i<=$str_length))

    {

    $temp_str=substr($sourcestr,$i,1);

    $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii碼

    if ($ascnum>=224) //如果ASCII位高與224,

    {

    $returnstr=$returnstr.substr($sourcestr,$i,3); //根據(jù)UTF-8編碼規(guī)范,將3個連續(xù)的字符計為單個字符

    $i=$i+3; //實際Byte計為3

    $n++; //字串長度計1

    }

    elseif ($ascnum>=192) //如果ASCII位高與192,

    {

    $returnstr=$returnstr.substr($sourcestr,$i,2); //根據(jù)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數(shù)仍計1個

    $n++; //但考慮整體美觀,大寫字母計成一個高位字符

    }

    else //其他情況下,包括小寫字母和半角標(biāo)點符號,

    {

    $returnstr=$returnstr.substr($sourcestr,$i,1);

    $i=$i+1; //實際的Byte數(shù)計1個

    $n=$n+0.5; //小寫字母和半角標(biāo)點等與半個高位字符寬...

    }

    }

    if ($str_length>$cutlength){

    $returnstr = $returnstr . "...";//超過長度時在尾處加上省略號

    }

    return $returnstr;

    }

    截取utf-8字符串函數(shù)

    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;

    for($i=$start;$i

    {

    $ctype = 0;

    $cstep = 0;

    $cur = substr($title,$i,1);

    if($cur == "&")

    {

    if(substr($title,$i,4) == "<")

    {

    $cstep = 4;

    $length += 4;

    $i += 3;

    $realnum ++;

    if($magic)

    {

    $alen ++;

    }

    }

    else if(substr($title,$i,4) == ">")

    {

    $cstep = 4;

    $length += 4;

    $i += 3;

    $realnum ++;

    if($magic)

    {

    $alen ++;

    }

    }

    else if(substr($title,$i,5) == "&")

    {

    $cstep = 5;

    $length += 5;

    $i += 4;

    $realnum ++;

    if($magic)

    {

    $alen ++;

    }

    }

    else if(substr($title,$i,6) == """)

    {

    $cstep = 6;

    $length += 6;

    $i += 5;

    $realnum ++;

    if($magic)

    {

    $alen ++;

    }

    }

    else if(preg_match("/&#(d+);?/i",substr($title,$i,8),$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){

    $cstep = 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);

    }

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:PHP截取中文字符串方法總結(jié)
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)