2009-07-09 44 views
12

我在變量中有以下字符串。如何從字符串中獲得前x個字符,而不切斷最後一個單詞?

 
Stack Overflow is as frictionless and painless to use as we could make it. 

我想先28個字符從上面的行提取,因此通常如果我用substr然後它會給我Stack Overflow is as frictio這個輸出,但我想輸出:

 
Stack Overflow is as... 

是否有任何預在PHP中這樣做的功能,或者請在PHP中爲我提供代碼?

編輯:

我想從字符串共有28個字符,不破壞一個字,如果它會返回我少一些字符超過28不破壞一個字,那很好。

+2

所以,你不是真的想要第28個字符,而是第28個字的長度是28。 – 2009-07-09 14:45:59

+1

是的,我想總共28個字符..沒有一個字,如果它會返回少於28個字符沒有一個字,沒關係,這很好... – Prashant 2009-07-09 14:47:02

+1

比確保這是明確指出的問題! – 2009-07-09 14:48:34

回答

49

可以使用wordwrap()功能,然後發生爆炸換行,並採取第一部分:

$str = wordwrap($str, 28); 
$str = explode("\n", $str); 
$str = $str[0] . '...'; 
-1

爲什麼不嘗試使它爆炸並得到數組的前4個元素?

0

嘗試:

$string='Stack Overflow is as frictionless and painless to use as we could make it.'; 
$n=28; 
$break=strpos(wordwrap($string, $n,'<<||>>'),'<<||>>'); 
print substr($string,0,($break==0?strlen($string):$break)).(strlen($string)>$n?'...':''); 

$string='Stack Overflow'; 
$n=28; 
$break=strpos(wordwrap($string, $n,'<<||>>'),'<<||>>'); 
print substr($string,0,($break==0?strlen($string):$break)).(strlen($string)>$n?'...':''); 
9

AlfaSky

function addEllipsis($string, $length, $end='…') 
{ 
    if (strlen($string) > $length) 
    { 
     $length -= strlen($end); 
     $string = substr($string, 0, $length); 
     $string .= $end; 
    } 

    return $string; 
} 

的替代,更多的其他功能實現從Elliott Brueggeman's blog

/** 
* trims text to a space then adds ellipses if desired 
* @param string $input text to trim 
* @param int $length in characters to trim to 
* @param bool $ellipses if ellipses (...) are to be added 
* @param bool $strip_html if html tags are to be stripped 
* @return string 
*/ 
function trim_text($input, $length, $ellipses = true, $strip_html = true) { 
    //strip tags, if desired 
    if ($strip_html) { 
     $input = strip_tags($input); 
    } 

    //no need to trim, already shorter than trim length 
    if (strlen($input) <= $length) { 
     return $input; 
    } 

    //find last space within length 
    $last_space = strrpos(substr($input, 0, $length), ' '); 
    $trimmed_text = substr($input, 0, $last_space); 

    //add ellipses (...) 
    if ($ellipses) { 
     $trimmed_text .= '...'; 
    } 

    return $trimmed_text; 
} 

(谷歌搜索: 「PHP裝飾省略號」)

3

這裏是你可以做的一種方式:

$str = "Stack Overflow is as frictionless and painless to use as we could make it."; 

$strMax = 28; 
$strTrim = ((strlen($str) < $strMax-3) ? $str : substr($str, 0, $strMax-3)."..."); 

//or this way to trim to full words 
$strFull = ((strlen($str) < $strMax-3) ? $str : strrpos(substr($str, 0, $strMax-3),' ')."..."); 
0

我會使用string tokenizer將字符串拆分爲如下所示的單詞:

$string = "Stack Overflow is as frictionless and painless to use as we could make it."; 
$tokenized_string = strtok($string, " "); 

然後你可以以任何你想要的方式拉出單詞。


編輯:格雷格有一個更好,更優雅的做你想做的方式。我會用他的wordwrap()解決方案。

2

這是我所知道的最簡單的解決方案...

substr($string,0,strrpos(substr($string,0,28),' ')).'...'; 
0

可以使用wordwrap

string wordwrap (string $str [, int $width= 75 [, string $break= "\n" [, bool $cut= false ]]]) 

-

function firstNChars($str, $n) { 
    return array_shift(explode("\n", wordwrap($str, $n))); 
} 

echo firstNChars("bla blah long string", 25) . "..."; 

免責聲明:沒有測試它。

此外,如果您的字符串包含\n s,它可能會更早打破。

0
function truncate($string, $limit, $break=" ", $pad="...") { 

// return with no change if string is shorter than $limit 
if(strlen($string) <= $limit){ 
    return $string; 
} 

$string = substr($string, 0, $limit); 
if(false !== ($breakpoint = strrpos($string, $break))){ 
    $string = substr($string, 0, $breakpoint); 
} 
return $string . $pad; 
} 
2

這是最簡單的方法:如果你的字符串有HTML標籤,& NBSP和多空間可以出現

<?php 
$title = "this is the title of my website!"; 
$number_of_characters = 15; 
echo substr($title, 0, strrpos(substr($title, 0, $number_of_characters), " ")); 
?> 
0

問題。以下是我使用需要照料一切:

function LimitText($string,$limit,$remove_html=0){ 
    if ($remove_html==1){$string=strip_tags($string);} 
    $newstring = preg_replace("/(?:\s|&nbsp;)+/"," ",$string, -1); // replace &nbsp with space 
    $newstring = preg_replace(array('/\s{2,}/','/[\t\n]/'),' ',$newstring); // replace duplicate spaces 
    if (strlen($newstring)<=$limit) { return $newstring; } // ensure length is more than $limit 
    $newstring = substr($newstring,0,strrpos(substr($newstring,0,$limit),' ')); 
    return $newstring; 
} 

用法:

$string = 'My wife is jealous of stackoverflow'; 
echo LimitText($string,20); 
// My wife is jealous 

使用的HTML:

$string = '<div><p>My wife is jealous of stackoverflow</p></div>'; 
echo LimitText($string,20,1); 
// My wife is jealous 
0

這的工作對我來說完美

function WordLimt($Keyword,$WordLimit){ 

    if (strlen($Keyword)<=$WordLimit) { return $Keyword; } 
    $Keyword= substr($Keyword,0,strrpos(substr($Keyword,0,$WordLimit),' ')); 
    return $Keyword; 
} 

echo WordLimt($MyWords,28); 

// OutPut : Stack Overflow is as 

它會在沒有最後一個空間的情況下調整和打破cut word ...

相關問題