我在变量中有以下字符串。
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而不会断言,那就没问题了。
答案 0 :(得分:51)
你可以使用wordwrap()
功能,然后在换行符上爆炸并采取第一部分:
$str = wordwrap($str, 28);
$str = explode("\n", $str);
$str = $str[0] . '...';
答案 1 :(得分:10)
来自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 trim ellipses”)
答案 2 :(得分: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),' ')."...");
答案 3 :(得分:2)
这是我所知道的最简单的解决方案......
substr($string,0,strrpos(substr($string,0,28),' ')).'...';
答案 4 :(得分:2)
这是最简单的方法:
<?php
$title = "this is the title of my website!";
$number_of_characters = 15;
echo substr($title, 0, strrpos(substr($title, 0, $number_of_characters), " "));
?>
答案 5 :(得分: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?'...':'');
答案 6 :(得分:0)
我会使用string tokenizer将字符串拆分成如下字样:
$string = "Stack Overflow is as frictionless and painless to use as we could make it.";
$tokenized_string = strtok($string, " ");
然后你可以随心所欲地抽出单词。
编辑:Greg有更好,更优雅的方式做你想做的事。我会使用他的wordwrap()解决方案。
答案 7 :(得分: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,它可能会更早被破坏。
答案 8 :(得分: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;
}
答案 9 :(得分:0)
如果您的字符串包含html标记,&amp; nbsp和多个空格,则可能会出现问题。以下是我用来处理所有事情的内容:
function LimitText($string,$limit,$remove_html=0){
if ($remove_html==1){$string=strip_tags($string);}
$newstring = preg_replace("/(?:\s| )+/"," ",$string, -1); // replace   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
答案 10 :(得分: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
它会调整并在最后一个空格上打破而不会被切断...
答案 11 :(得分:-1)
为什么不尝试将它爆炸并获得数组的前4个元素?
答案 12 :(得分:-1)
substr("some string", 0, x);