PHP切割漢字

2022-02-01 00:46:29 字數 3007 閱讀 2329

<?php

/*@utf-8編碼的字元可能由1~3個位元組組成。

*//*

--------------------------方法一擷取中文字串方法------------------------------

*/function msubstr($str, $start, $len)

else

}return

$tmpstr;}

/*----------------------------第二種方法-----------------------------------

*///

擷取的是utf-8字串

function utf_substr($str, $len

) }

else

}return

join($new_str);//

join()函式把陣列元素組合為乙個字串}/*

-------------------------------------第三種方法(utf-8)--------------------------------

*/function cutstr($string, $length

) }

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 == "")'.

// '((?:[\x00-\x7f]|[\xc0-\xff][\x80-\xbf]+)).*#s',

// '$1',$name1);

// }

/*--------------------------------------------第四種方法(utf-8)---------------------------------------------

*/function cut_str($sourcestr, $cutlength

) elseif ($ascnum >= 192)//

如果ascii位高於192

elseif ($ascnum >= 65 && $ascnum

<= 90)//

如果是大寫字母

else

}if ($str_length > $cutlength

)

return

$returnstr;}

/*--------------------第五種方法(utf-8)---------------------------------------------

*/function fsubstr($title, $start, $len = "", $magic = true

)

$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++)

} elseif (substr($title, $i, 4) == ">")

} elseif (substr($title, $i, 5) == "&")

} elseif (substr($title, $i, 6) == """)

} elseif (preg_match("/&#(\d+);?/i", substr($title, $i), $match

)) }

} else

} elseif (ord($cur) >= 248)

} elseif (ord($cur) >= 240)

} elseif (ord($cur) >= 224)

} elseif (ord($cur) >= 192)

} elseif (ord($cur) >= 128)

else

else}}

}if ($magic

) else}}

else

}unset($cur

);

unset($alen

);

unset($blen

);

unset($realnum

);

unset($ctype

);

unset($cstep

);

return

substr($title, $start, $length

);}

function utf8substr($str, $from, $len

)' .

'((?:[\x00-\x7f]|[\xc0-\xff][\x80-\xbf]+)).*#s',

'$1', $str);}

$title = "你哈珀niad1納斯達wop asdni你愛誰都沒阿斯頓撒旦12ccs- sd";

$title = utf8substr($title, 0, 15);

echo

$title;

?>

php漢字轉拼音 漢字轉拼音 用PHP實現

每日17點準時技術乾貨分享 php實現漢字轉拼音 php漢字轉拼音我使用到了overtrue pinyin拓展 一 安裝overtrue pinyin拓展 composer require overtrue pinyin二 overtrue pinyin拓展簡單使用overtrue pinyin拼音...

PHP版 漢字轉碼

function unicode encode str,encoding gbk prefix postfix return unistr str 哈哈 unistr unicode encode str echo unistr.以上 以下 漢字亂碼一般用gb和utf 8,如果用gb編碼,網頁用ut...

PHP 漢字轉拼音

php 漢字轉拼音 author jerryli hzjerry gmail.com version v0.20140715 package spfw.core.lib.final global sea php fw var env example echo cutf8 py encode 阿里巴巴...