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

前端 未结 9 1547
忘了有多久
忘了有多久 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:58

    Using substr_count to Count the number of any substring occurrences. for finding number of words set $needle to ' '. int substr_count ( string $haystack , string $needle)

    $text = 'This is a test';
    echo substr_count($text, 'is'); // 2
    
    
    echo substr_count($text, ' ');// return number of occurance of words
    

提交回复
热议问题