如何统计所有带有特殊字符的单词
How to count all the words with special characters
我有一个关于字符串的问题,我想计算字符串中的所有字符。就像我有一个字符串
"Hello world & good morning. The date is 18.05.2016"
str_word_count 的第 3 个参数允许您设置额外的字符来计算为单词:
str_word_count($document, 0, '&.0..9');
&.0..9
表示它将考虑 &
、.
,范围从 0
到 9
。
您可以使用explode()
将字符串转换为数组,然后使用count()
函数计算数组的长度。
echo count(explode(' ', "Hello world & good morning. The date is 18.05.2016"))
您可以试试这个代码。
<?php
$file = "C:\Users\singh\Desktop\Talkative Challenge\base_example.txt";
$document = file_get_contents($file);
$return_array = preg_split("/[\s,]+/",$document);
echo count($return_array);
echo $document;
?>
希望一切顺利。
您可以用 substr_count 计算空格并加一。
echo substr_count($str, " ")+1;
// 9
我有一个关于字符串的问题,我想计算字符串中的所有字符。就像我有一个字符串
"Hello world & good morning. The date is 18.05.2016"
str_word_count 的第 3 个参数允许您设置额外的字符来计算为单词:
str_word_count($document, 0, '&.0..9');
&.0..9
表示它将考虑 &
、.
,范围从 0
到 9
。
您可以使用explode()
将字符串转换为数组,然后使用count()
函数计算数组的长度。
echo count(explode(' ', "Hello world & good morning. The date is 18.05.2016"))
您可以试试这个代码。
<?php
$file = "C:\Users\singh\Desktop\Talkative Challenge\base_example.txt";
$document = file_get_contents($file);
$return_array = preg_split("/[\s,]+/",$document);
echo count($return_array);
echo $document;
?>
希望一切顺利。
您可以用 substr_count 计算空格并加一。
echo substr_count($str, " ")+1;
// 9