说我有以下内容:
$name= "albert slevir inshten";
Run Code Online (Sandbox Code Playgroud)
对于上述,我会得到3.
PHP提供了一个函数substr_count来计算传入的子字符串的出现次数:
$count = substr_count($str, ' '); //2
Run Code Online (Sandbox Code Playgroud)
要检索单词数,您可以使用str_word_count:
$count = str_word_count($str); //3
Run Code Online (Sandbox Code Playgroud)