how to count the words in a specific string in PHP?

前端 未结 9 1570
忘了有多久
忘了有多久 2020-12-18 21:45

I want to count the words in a specific string , so I can validate it and prevent users to write more than 100 words for example .

I wrote this function but I don\'t

9条回答
  •  渐次进展
    2020-12-18 21:48

    There are n-1 spaces between n objects so there will be 99 spaces between 100 words, so u can choose and average length for a word say for example 10 characters, then multiply by 100(for 100 words) then add 99(spaces) then you can instead make the limitation based on number of characters(1099).

    function isValidLength($text){
    

    if(strlen($text) > 1099)

         return false;
    

    else return true;

    }

提交回复
热议问题