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

前端 未结 11 2025
我在风中等你
我在风中等你 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 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 "...";
    

提交回复
热议问题