How to get first x chars from a string, without cutting off the last word?

后端 未结 13 1275
再見小時候
再見小時候 2020-11-28 10:24

I have the following string in a variable.

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

I want to fetch first 28 characte

相关标签:
13条回答
  • 2020-11-28 10:33

    Problems can arise if your string has html tags, &nbsp and multiple spaces. Here is what I use that takes care of everything:

    function LimitText($string,$limit,$remove_html=0){
        if ($remove_html==1){$string=strip_tags($string);}
        $newstring = preg_replace("/(?:\s| )+/"," ",$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;
    }
    

    usage:

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

    usage with html:

    $string = '<div><p>My wife is jealous of stackoverflow</p></div>';
    echo LimitText($string,20,1);
    // My wife is jealous
    
    0 讨论(0)
  • 2020-11-28 10:34

    This is the easiest way:

    <?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 讨论(0)
  • 2020-11-28 10:34

    This is the simplest solution I know of...

    substr($string,0,strrpos(substr($string,0,28),' ')).'...';
    
    0 讨论(0)
  • 2020-11-28 10:38

    try:

    $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?'...':'');
    
    0 讨论(0)
  • 2020-11-28 10:42
    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;
    }
    
    0 讨论(0)
  • 2020-11-28 10:42

    This's Working for me Perfect

    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
    

    it will adjust and break on last Space without cut word...

    0 讨论(0)
提交回复
热议问题