我試圖想出以下功能截斷字符串全字(如果可能的話,否則它應該截斷到個字符):合併兩個正則表達式來截斷字符串裏的單詞
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愚弄 - 我應該把它留到另一天,現在睡一覺。今天關閉。
可憐的狐狸。 _____ – kennytm 2010-04-21 13:15:31
爲什麼不使用'trim'來擺脫尾隨的空白? – Jens 2010-04-21 13:34:27
喚醒40小時並處理正則表達式。 +1可憐的票。 – 2010-04-21 13:36:10