Cd Chen's Services

ba ba ba la~~

PHP 中文斷行的處理方法

今天在 Joomla! 中,安裝了 mod_janews 與 ja_teline,畫面很漂亮,超級滿意的。JoomlArt 總算搞出一個比較漂亮的 template 了。

不過,mod_janews 會有一個缺點,就是無法 (沒有) 處理中文字的斷行問題,這樣會造成在不該的地方亂斷行。例如:

應該在這裡斷行的,卻斷在?...

後來翻到 "修復 recent comments 把中文字切一半的問題" 這篇文章,參考其中的作法,稍作修改。現在,總算可以正確的斷行了!!

檢附程式碼如下:


--- mod_janews.php.orig 2007-04-20 04:27:41.000000000 +0800
+++ mod_janews.php 2007-04-20 04:18:00.000000000 +0800
@@ -12,8 +12,12 @@
$row->introtext = preg_replace( $regex, '', $row->introtext );

$row->introtext1 = strip_tags($row->introtext);
- if ($maxchars && strlen ($row->introtext) > $maxchars) {
- $row->introtext1 = mb_substr ($row->introtext1, 0, $maxchars) . "...";
+ // Cd Chen: For process Chinese word breaking...
+ // if ($maxchars && strlen ($row->introtext) > $maxchars) {
+ if ($maxchars && mb_strlen ($row->introtext) > $maxchars) {
+ // Cd Chen: For process Chinese word breaking...
+ // $row->introtext1 = mb_substr ($row->introtext1, 0, $maxchars) . "...";
+ $row->introtext1 = mb_strimwidth ($row->introtext1, 0, $maxchars, "...", "utf-8");
}
// clean up globals
return $image;

這篇內容的 Trackback 網址:

http://cdchen.idv.tw/trackback/408