有人可以向我解释这个 'counting sentences' php 代码吗?
Can someone explain to me this 'counting sentences' php code?
我有一个不使用str_word_count
来数句子的任务,学长给我的,我看不懂。有人可以解释一下吗?
我需要了解变量及其工作原理。
<?php
$sentences = "this book are bigger than encyclopedia";
function countSentences($sentences) {
$y = "";
$numberOfSentences = 0;
$index = 0;
while($sentences != $y) {
$y .= $sentences[$index];
if ($sentences[$index] == " ") {
$numberOfSentences++;
}
$index++;
}
$numberOfSentences++;
return $numberOfSentences;
}
echo countSentences($sentences);
?>
输出为
6
基本上就是统计一个句子中space的个数
<?php
$sentences = "this book are bigger than encyclopedia";
function countSentences($sentences) {
$y = ""; // Temporary variable used to reach all chars in $sentences during the loop
$numberOfSentences = 0; // Counter of words
$index = 0; // Array index used for $sentences
// Reach all chars from $sentences (char by char)
while($sentences != $y) {
$y .= $sentences[$index]; // Adding the current char in $y
// If current char is a space, we increase the counter of word
if ($sentences[$index] == " "){
$numberOfSentences++;
}
$index++; // Increment the index used with $sentences in order to reach the next char in the next loop round
}
$numberOfSentences++; // Additional incrementation to count the last word
return $numberOfSentences;
}
echo countSentences($sentences);
?>
请注意,此函数在某些情况下会产生错误结果,例如,如果后面有两个 space,此函数将计算 2 个单词而不是一个。
我会说这是非常微不足道的事情。
任务是计算句子中的单词。句子是一个字符串(字符序列),是字母或白色spaces(space,换行等)...
现在,句子中的一个词是什么? "don't touch" 另一组字母是一组不同的字母;意思是单词(一组字母)用白色 space 相互分隔(假设只是一个普通的空白 space)
所以最简单的单词计数算法包括:
- $words_count_variable = 0
- 逐个检查所有角色
- 每次你找到一个space,这意味着一个新词刚刚结束,你必须增加你的$words_count_variable
- 最后,您会找到字符串的末尾,这意味着在此之前刚刚结束的一个词,因此您将最后一次增加 $words_count_variable
取"this is a sentence".
We set $words_count_variable = 0;
Your while cycle will analyze:
"t"
"h"
"i"
"s"
" " -> blank space: a word just ended -> $words_count_variable++ (becomes 1)
"i"
"s"
" " -> blank space: a word just ended -> $words_count_variable++ (becomes 2)
"a"
" " -> blank space: a word just ended -> $words_count_variable++ (becomes 3)
"s"
"e"
"n"
...
"n"
"c"
"e"
-> end reached: a word just ended -> $words_count_variable++ (becomes 4)
那么,4。
共计 4 个字。
希望这对您有所帮助。
我有一个不使用str_word_count
来数句子的任务,学长给我的,我看不懂。有人可以解释一下吗?
我需要了解变量及其工作原理。
<?php
$sentences = "this book are bigger than encyclopedia";
function countSentences($sentences) {
$y = "";
$numberOfSentences = 0;
$index = 0;
while($sentences != $y) {
$y .= $sentences[$index];
if ($sentences[$index] == " ") {
$numberOfSentences++;
}
$index++;
}
$numberOfSentences++;
return $numberOfSentences;
}
echo countSentences($sentences);
?>
输出为
6
基本上就是统计一个句子中space的个数
<?php
$sentences = "this book are bigger than encyclopedia";
function countSentences($sentences) {
$y = ""; // Temporary variable used to reach all chars in $sentences during the loop
$numberOfSentences = 0; // Counter of words
$index = 0; // Array index used for $sentences
// Reach all chars from $sentences (char by char)
while($sentences != $y) {
$y .= $sentences[$index]; // Adding the current char in $y
// If current char is a space, we increase the counter of word
if ($sentences[$index] == " "){
$numberOfSentences++;
}
$index++; // Increment the index used with $sentences in order to reach the next char in the next loop round
}
$numberOfSentences++; // Additional incrementation to count the last word
return $numberOfSentences;
}
echo countSentences($sentences);
?>
请注意,此函数在某些情况下会产生错误结果,例如,如果后面有两个 space,此函数将计算 2 个单词而不是一个。
我会说这是非常微不足道的事情。 任务是计算句子中的单词。句子是一个字符串(字符序列),是字母或白色spaces(space,换行等)...
现在,句子中的一个词是什么? "don't touch" 另一组字母是一组不同的字母;意思是单词(一组字母)用白色 space 相互分隔(假设只是一个普通的空白 space)
所以最简单的单词计数算法包括: - $words_count_variable = 0 - 逐个检查所有角色 - 每次你找到一个space,这意味着一个新词刚刚结束,你必须增加你的$words_count_variable - 最后,您会找到字符串的末尾,这意味着在此之前刚刚结束的一个词,因此您将最后一次增加 $words_count_variable
取"this is a sentence".
We set $words_count_variable = 0;
Your while cycle will analyze:
"t"
"h"
"i"
"s"
" " -> blank space: a word just ended -> $words_count_variable++ (becomes 1)
"i"
"s"
" " -> blank space: a word just ended -> $words_count_variable++ (becomes 2)
"a"
" " -> blank space: a word just ended -> $words_count_variable++ (becomes 3)
"s"
"e"
"n"
...
"n"
"c"
"e"
-> end reached: a word just ended -> $words_count_variable++ (becomes 4)
那么,4。 共计 4 个字。
希望这对您有所帮助。