Count all word including numbers in a php string

前端 未结 8 1165
梦谈多话
梦谈多话 2021-02-15 17:01

To count words in a php string usually we can use str_word_count but I think not always a good solution

good example:

$var =\"Hello world!\";
echo str_         


        
8条回答
  •  时光说笑
    2021-02-15 17:54

    ans:

    function limit_text($text, $limit) {
        if(str_word_count($text, 0) > $limit) {
            $words = str_word_count($text, 2);
            $pos = array_keys($words);
            $text = substr($text, 0, $pos[$limit]) . '...';
        }
        return $text;
    }
    

提交回复
热议问题