2010-04-21 35 views
7

我試圖想出以下功能截斷字符串全字(如果可能的話,否則它應該截斷到個字符):合併兩個正則表達式來截斷字符串裏的單詞

function Text_Truncate($string, $limit, $more = '...') 
{ 
    $string = trim(html_entity_decode($string, ENT_QUOTES, 'UTF-8')); 

    if (strlen(utf8_decode($string)) > $limit) 
    { 
     $string = preg_replace('~^(.{1,' . intval($limit) . '})(?:\s.*|$)~su', '$1', $string); 

     if (strlen(utf8_decode($string)) > $limit) 
     { 
      $string = preg_replace('~^(.{' . intval($limit) . '}).*~su', '$1', $string); 
     } 

     $string .= $more; 
    } 

    return trim(htmlentities($string, ENT_QUOTES, 'UTF-8', true)); 
} 

這裏有一些測試:

// Iñtërnâtiônàlizætiøn and then the quick brown fox... (49 + 3 chars) 
echo dyd_Text_Truncate('Iñtërnâtiônàlizætiøn and then the quick brown fox jumped overly the lazy dog and one day the lazy dog humped the poor fox down until she died.', 50, '...'); 

// Iñtërnâtiônàlizætiøn_and_then_the_quick_brown_fox_... (50 + 3 chars) 
echo dyd_Text_Truncate('Iñtërnâtiônàlizætiøn_and_then_the_quick_brown_fox_jumped_overly_the_lazy_dog and one day the lazy dog humped the poor fox down until she died.', 50, '...'); 

他們都工作,因爲它是,但如果我把第二preg_replace()我得到如下:

Iñtërnâtiônàlizætiøn_and_then_the_quick_brown_fox_jumped_overly_the_lazy_dog 有一天懶狗弓起的 可憐的狐狸,直到她去世....

我不能使用substr(),因爲它只能在字節級和我沒有獲得mb_substr()自動櫃員機,我已經做了幾次嘗試加入第一個正則表達式,但沒有成功。

請幫助S.M.S.,我一直在爲此奮鬥了近一個小時。


編輯:對不起,我已經清醒40小時,我無恥地錯過了這一點:

$string = preg_replace('~^(.{1,' . intval($limit) . '})(?:\s.*|$)?~su', '$1', $string); 

不過,如果有人有更優化的正則表達式(或一個忽略尾隨空格),請分享一下:

"Iñtërnâtiônàlizætiøn and then " 
"Iñtërnâtiônàlizætiøn_and_then_" 

編輯2:我仍然無法擺脫尾隨空白的,有人可以幫我嗎?編輯3:好的,我的編輯沒有真正起作用,我被RegexBuddy愚弄 - 我應該把它留到另一天,現在睡一覺。今天關閉。

+3

可憐的狐狸。 _____ – kennytm 2010-04-21 13:15:31

+0

爲什麼不使用'trim'來擺脫尾隨的空白? – Jens 2010-04-21 13:34:27

+3

喚醒40小時並處理正則表達式。 +1可憐的票。 – 2010-04-21 13:36:10

回答

3

也許我可以一個漫長的夜晚正則表達式的噩夢後,給你一個快樂的上午:

'~^(.{1,' . intval($limit) . '}(?<=\S)(?=\s)|.{'.intval($limit).'}).*~su' 

沸騰了下去:

​​

你總是可以添加|$到底(?=\s)但由於您的代碼已經檢查字符串的長度是否比$limit更長,我不覺得這種情況是必要的。

+0

更多「愉快的下午」,但感謝gnarf!我去睡覺有一個印象,我將不得不使用lookahead或只使用trim()'。再次感謝! – 2010-04-22 18:00:43

0

您是否考慮過使用wordwrap? (http://us3.php.net/wordwrap

+1

是的,對於大字符串,它的速度較慢,不適用於多字節字符。 – 2010-04-22 18:15:20

相關問題