PALMisLIFE 討論區

搜索
鹹魚爸魅力四射舞蹈教室
查看: 17987|回復: 0
打印 上一主題 下一主題

[PHP] 中英文排版調整功能

[複製鏈接]

3867

主題

126

好友

2624

積分

  • TA的每日心情

    2011-4-12 00:15
  • 簽到天數: 3 天

    連續簽到: 2 天

    [LV.2]偶爾看看I

    文章
    35006
    跳轉到指定樓層
    1#
    發表於 2008-5-29 10:13 |只看該作者 |倒序瀏覽
    花了一點時間和請多位朋友幫忙協助下寫出來的,應該算可用了
    其實功能需求很簡單,只是碰上 Big5 編碼時,單純的切字會造成很多問題,因此大部分的時間其實都是在解決 Big5 編碼上會造成的問題

    相關原因建議推薦參考  提倡易讀、標準的文章標題



    [PHP 英文中文排版程式]

    英文與中文之間自動加空白,例如 測試test測試 --> 測試 test 測試
    .,!? 等特殊符號與中文之間加空白,但是只加在後面,例如 測試!測試 --> 測試! 測試
    [](){} 等對等符號與中文之間加空白,但只加在外側,例如 測試[測試]測試 --> 測試 [測試] 測試



    1. // 檢查是否為中文字,並分左右,為中文字左側輸出 -1 ,右側輸出 0

    2. function is_chinese(&$str, $location) {

    3. $ch = true;  
    4. $i = $location;  

    5. while(ord($str[$i])>0xa0 && $i >= 0) {  

    6.   $ch = !$ch;  
    7.   $i --;  
    8. }  

    9. if($i != $location) {  

    10. $f_str = $ch ? 1: -1;  

    11. } else {  
    12.   
    13.   $f_str = false;  

    14. }  

    15. return $f_str;  

    16. }  

    17. //  [PHP 英文中文排版程式]

    18. // 英文與中文之間自動加空白,例如 測試test測試 --> 測試 test 測試
    19. // .,!? 等特殊符號與中文之間加空白,但是只加在後面,例如 測試!測試 --> 測試! 測試
    20. // [](){} 等對等符號與中文之間加空白,但只加在外側,例如 測試[測試]測試 --> 測試 [測試] 測試

    21. function add_space($str) {   

    22. $L = '\(\[\{\<';
    23. $R = '\)\]\}\>\,\.\!\,\?\;\:';
    24. $change = '/^[a-zA-Z0-9_,.!?'.$L.$R.']$/';

    25. $len = strlen($str);

    26. for($i = 0;$i <$len;$i++)     {         

    27.   $j = $i + 1;
    28.   
    29.   if (is_chinese($str, $i) == "-1") {
    30.   
    31.   $result_str .= substr($str,$i,2);
    32.   $nextpass = 1; // 設定已取兩字元,下一迴圈就不要重複取字
    33.   
    34.   } else {

    35.    // 檢查是否有空白、左字元、右字元,有的話就略過處理
    36.    if ((substr($str,$i,1) == " ")
    37.        OR (substr($str,$j,1) == " ")
    38.     OR (preg_match('/^['.$L.']$/', substr($str,$i,1)))
    39.     OR (preg_match('/^['.$R.']$/', substr($str,$j,1)))
    40.     )
    41.    {
    42.     if ($nextpass == 1) { $nextpass = 0;} else {$result_str .= substr($str,$i,1);}

    43.    } else {
    44.    
    45.       $itest = preg_match($change, substr($str,$i,1));
    46.       $jtest = preg_match($change, substr($str,$j,1));
    47.       
    48.       // 這是避免中文第二字是英文造成的錯誤
    49.       if ($nextpass == 1) {$itest = 0;}

    50.     if ($itest XOR $jtest) {
    51.    
    52.      if ($nextpass == 1) { $nextpass = 0;} else {$result_str .= substr($str,$i,1);}
    53.      $result_str .= " ";
    54.      //echo "!";
    55.      //echo "a=".preg_match($change, substr($str,$i,1)).";b=".preg_match($change, substr($str,$j,1));
    56.      //echo substr($str,$i,1);
    57.      //echo strlen(is_chinese($str, $i).is_chinese($str, $j));

    58.     } else {

    59.     if ($nextpass == 1) { $nextpass = 0;} else {$result_str .= substr($str,$i,1);}
    60.     }
    61.    }
    62.   }
    63. }
    64. return $result_str;
    65. }

    複製代碼
    分享淘帖0 分享分享0 收藏收藏0 頂0 踩0
    您需要登錄後才可以回帖 登錄 | 免費註冊

    與站長聯繫| PALMisLIFE 掌上生活      下載:更快、更棒、更好玩

    GMT+8, 2024-4-25 18:35 , Processed in 0.043036 second(s), 30 queries , Gzip On.

    Powered by Discuz!

    © 2001-2012 Comsenz Inc. style by eisdl

    回頂部