引用 | 編輯
fishingboy
2005-08-11 23:49 |
樓主
▼ |
||
x0
我用substr($str,0,100)去取固定長度的字串時常常會有中文字被切一半的情形? 所以最後那個字就變成亂碼了? 有什麼方法可以解決這個問題嗎? 麻煩各位大大了! 謝謝哦~~ x0
|
引用 | 編輯
fishingboy
2005-08-15 00:33 |
2樓
▲ ▼ |
謝啦~~
不過公司的伺服器沒有支援耶~~ 看來要再找資料了... x0 |
引用 | 編輯
fishingboy
2005-08-16 21:04 |
4樓
▲ ▼ |
下面是引用xxxzzz於2005-08-15 01:11發表的 : 謝謝囉! 非常感謝! x0 |
引用 | 編輯
f40net
2005-09-03 05:33 |
5樓
▲ ▼ |
若使用UTF-8編碼,可用下面的Funtion,可避免中文字被切掉一半。
複製程式 <? 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<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]);; } //使用範例: $text = "撰寫網誌內容記錄生活感想、嗜好、心情故事、或珍貴回憶。"; $text = cutstr($text,20); //(原字串,截取長度) echo "$text"; ?> x0 |