Trim text to 340 chars

前端 未结 9 1067
情深已故
情深已故 2021-02-07 14:21

I\'m pulling blog posts from a DB. I want to trim the text to a max length of 340 characters.

If the blog post is over 340 characters I want to trim the text to the las

9条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-07 14:47

    Simplest solution

    $text_to_be_trim= "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard.";
    if(strlen($text_to_be_trim) > 20)   
        $text_to_be_trim= substr($text_to_be_trim,0,20).'....';
    

    For multi-byte text

    $stringText= "UTIL CONTROL DISTRIBUCION AMARRE CIGÜEÑAL";
    $string_encoding = 'utf8';
    $s_trunc =  mb_substr($stringText, 0, 37, $string_encoding);
    echo $s_trunc;
    

提交回复
热议问题