How do you cut off text after a certain amount of characters in PHP?

前端 未结 11 1865
我在风中等你
我在风中等你 2021-02-02 12:22

I have two string that i want to limit to lets say the first 25 characters for example. Is there a way to cut off text after the 25th character and add a ... to the end of the s

相关标签:
11条回答
  • 2021-02-02 12:58

    To avoid cutting right in the middle of a word, you might want to try the wordwrap function ; something like this, I suppose, could do :

    $str = "this is a long string that should be cut in the middle of the first 'that'";
    $wrapped = wordwrap($str, 25);
    var_dump($wrapped);
    
    $lines = explode("\n", $wrapped);
    var_dump($lines);
    
    $new_str = $lines[0] . '...';
    var_dump($new_str);
    

    $wrapped will contain :

    string 'this is a long string
    that should be cut in the
    middle of the first
    'that'' (length=74)
    

    The $lines array will be like :

    array
      0 => string 'this is a long string' (length=21)
      1 => string 'that should be cut in the' (length=25)
      2 => string 'middle of the first' (length=19)
      3 => string ''that'' (length=6)
    

    And, finally, your $new_string :

    string 'this is a long string' (length=21)
    


    With a substr, like this :

    var_dump(substr($str, 0, 25) . '...');
    

    You'd have gotten :

    string 'this is a long string tha...' (length=28)
    

    Which doesn't look that nice :-(


    Still, have fun !

    0 讨论(0)
  • 2021-02-02 13:04

    http://php.net/manual/en/function.mb-strimwidth.php (PHP 4 >= 4.0.6, PHP 5, PHP 7)

    <?php
    echo mb_strimwidth("Hello World", 0, 10, "...");
    echo "<br />";
    echo mb_strimwidth("Hello", 0, 10, "...");
    ?>
    

    output:

    Hello W...
    Hello
    
    0 讨论(0)
  • 2021-02-02 13:05

    substr function is the right one for you

    0 讨论(0)
  • 2021-02-02 13:09
     echo substr($str,0,25)."...";
    

    Would do the trick, but if you are dealing with words, you might want to cut off on word boundries so you don't have partial word doig this: The quick bl...

    So to do that (crude off the top of my head):

    $words = split(" ", strlen($str));
    
    for($i = 0,$j=0; $i< 25 && $j < sizeof($words) ;$j++)
    {
        $i += strlen($words[$j]);
        echo $words[$j]. " ";    
    }
    echo "...";
    
    0 讨论(0)
  • 2021-02-02 13:10
    $words=explode(' ', $string);
    for($ii = 0,$j=0; $ii< 5 && $j < sizeof($words) ;$j++){
        $ii += strlen($words[$j]);
        $intro= $words[$j]. " ";    
    }
    $intro.= "...";
    

    I know its to late, but if anyone else is returning to this page, this will do,

    0 讨论(0)
  • 2021-02-02 13:11

    I would go with Pascal MARTIN's answer, with some additional improvements. Because;

    1 - "PHP wordwrap", respects word boundaries automatically

    2 - If your string contains a word more than 25 chars(aagh, so there is nothing to do) you can force "PHP wordwrap" to cut word.(improvement)

    3 - If your string is shorter than 25 chars, then "..." will seem ugly after a complete sentence.(improvement)

    $str = "This string might be longer than 25 chars, or might not!";
    // we are forcing to cut long words...
    $wrapped = wordwrap($str, 25, "\n", 1);
    var_dump($wrapped);
    
    $lines = explode("\n", $wrapped);
    var_dump($lines);
    
    // if our $str is shorter than 25, there will be no $lines[1]
    (array_key_exists('1', $lines)) ? $suffix = '...' : $suffix = '';
    $new_str = $lines[0] . $suffix;
    var_dump($new_str);
    
    0 讨论(0)
提交回复
热议问题