[英]Count all word including numbers in a php string
To count words in a php string usually we can use str_word_count but I think not always a good solution要计算 php 字符串中的单词,通常我们可以使用 str_word_count 但我认为这并不总是一个好的解决方案
$var ="Hello world!";
echo str_word_count($str);
print_r(str_word_count($str, 1));
-->output --> 输出
2
Array ( [0] => Hello [1] => world )
$var ="The example number 2 is a bad example it will not
count numbers and punctuations !!";
-->output: --> 输出:
14
Array ( [0] => The [1] => example [2] => number [3] => is [4] => a
[5] => bad [6] => example [7] => it [8] => will [9] => not
[10] => count [11] => numbers [12] => and [13] => punctuations )
Is there a good predefined function to do this properly or do I have to use preg_match() ?是否有一个很好的预定义函数可以正确执行此操作,还是必须使用 preg_match() ?
You can always split your string by whitespace and count the results:您始终可以按空格拆分字符串并计算结果:
$res = preg_split('/\s+/', $input);
$count = count($res);
With your string用你的字符串
"The example number 2 is a bad example it will not
count numbers and punctuations !!"
This code will produce 16
.此代码将产生
16
。
The advantage of using this over explode(' ', $string)
is that it will work on multi-line strings as well as tabs, not just spaces.使用它而不是
explode(' ', $string)
的优点是它可以处理多行字符串以及制表符,而不仅仅是空格。 The disadvantage is that it's slower.缺点是比较慢。
The following using count()
and explode()
, will echo:以下使用
count()
和explode()
,将回显:
The number 1 in this line will counted and it contains the following count 8
PHP: PHP:
<?php
$text = "The number 1 in this line will counted";
$count = count(explode(" ", $text));
echo "$text and it contains the following count $count";
?>
Edit:编辑:
Sidenote:边注:
The regex can be modified to accept other characters that are not included in the standard set.可以修改正则表达式以接受标准集中未包含的其他字符。
<?php
$text = "The numbers 1 3 spaces and punctuations will not be counted !! . . ";
$text = trim(preg_replace('/[^A-Za-z0-9\-]/', ' ', $text));
$text = preg_replace('/\s+/', ' ', $text);
// used for the function to echo the line of text
$string = $text;
function clean($string) {
return preg_replace('/[^A-Za-z0-9\-]/', ' ', $string);
}
echo clean($string);
echo "<br>";
echo "There are ";
echo $count = count(explode(" ", $text));
echo " words in this line, this includes the number(s).";
echo "<br>";
echo "It will not count punctuations.";
?>
The most wide-spread method of counting words in a string is by splitting with any kind of whitespace:计算字符串中单词最广泛的方法是用任何类型的空格分割:
count(preg_split('~\s+~u', trim($text)))
Here, '~\\s+~u'
splits the whole text with any 1 or more Unicode whitespace characters.这里,
'~\\s+~u'
用任意 1 个或多个 Unicode 空白字符分割整个文本。
The disadvantage is that !!
缺点是
!!
is considered a word.被认为是一个词。
In case you want to count letter and number words (ie strings of text that are only made up of just letters or just numbers) you should consider a preg_match_all
like如果您想计算字母和数字单词(即仅由字母或数字组成的文本字符串),您应该考虑
preg_match_all
类的
if (preg_match_all('~[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?|\d+|(?>\p{L}\p{M}*+)+~u', $text, $matches)) {
return count($matches[0]);
}
See the regex demo and the PHP demo :请参阅正则表达式演示和PHP 演示:
$re = '~[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?|\d+|(?>\p{L}\p{M}*+)+~u';
$text = "The example number 2 is a bad example it will not \ncount numbers and punctuations !! X is 2.5674.";
if (preg_match_all($re, $text, $matches)) {
echo count($matches[0]);
} // 18 in this string
The [-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?
[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?
regex is a well-known integer or float number regex , and (?>\\p{L}\\p{M}*+)+
matches any 1 or more letters ( \\p{L}
), each of which can be followed with any amount of diacritic marks ( \\p{M}*+
). regex 是众所周知的整数或浮点数 regex ,并且
(?>\\p{L}\\p{M}*+)+
匹配任何 1 个或多个字母 ( \\p{L}
),每个字母都可以跟随带有任意数量的变音符号( \\p{M}*+
)。
Regex details正则表达式详情
[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?
- an optional -
or +
, 0+ ASCII digits, an optional .
-
或+
, 0+ ASCII 数字,一个可选的.
, 1+ ASCII digits, an optional sequence of e
or E
, an optional -
or +
and then 1+ ASCII digits e
或E
序列,可选的-
或+
然后是 1+ ASCII 数字|
- or \\d+
- any 1 or more Unicode digits \\d+
- 任何 1 个或多个 Unicode 数字|
- or (?>\\p{L}\\p{M}*+)+
- 1 or more occurrences of any Unicode letter followed with any 0+ diacritic symbols. (?>\\p{L}\\p{M}*+)+
- 任何 Unicode 字母出现 1 次或多次,后跟任何 0+ 变音符号。 In case you just want to count text chunks consisting of solely digits and letters (with diacritics) mixed up in any order , you may also use如果您只想计算以任何顺序混合的仅由数字和字母(带有变音符号)组成的文本块,您也可以使用
'~[\p{N}\p{L}\p{M}]+~u'
See another regex demo , \\p{M}
matches diacritics, \\p{N}
matches digits and \\p{L}
matches letters.参见另一个正则表达式演示,
\\p{M}
匹配变音符号, \\p{N}
匹配数字,而\\p{L}
匹配字母。
使用count(explode(' ', $var));
You can try this, 你可以试试看
<?php
function word_count($sentence)
{
$break = explode(" ",$sentence);
$count = count($break);
return $count;
}
$count = "Count all words of this sentence";
echo word_count($count);
//Output 6
?>
Here is more about Word Count In PHP 这是有关PHP中的字数统计的更多信息
You may also use the below code it's working for me.您也可以使用下面的代码,它对我有用。
function get_num_of_words($string) {
$string = preg_replace('/\s+/', ' ', trim($string));
$words = explode(" ", $string);
return count($words);
}
$string="php string word count in simple way";
echo $count=get_num_of_words($string);
The result will be 7结果将是 7
I know the question is old, Still im sharing the fix i have adopt for this.我知道这个问题很老,我仍然分享我为此采用的修复程序。
$str ="Hello world !";
// you can include allowed special characters as third param.
print_r(str_word_count($str, 1, '!'));
code output is代码输出是
Array ( [0] => Hello [1] => world [2] => ! )
if you want to include more words u can specify as third param.如果您想包含更多单词,您可以指定为第三个参数。
print_r(str_word_count($str, 1, '0..9.~!@#$%^&*()-_=+{}[]\|;:?/<>.,'));
from 0..9.从 0..9. will include all numbes, and other special characters are inserted individually.
将包括所有数字,并单独插入其他特殊字符。
Just some improvement your solution只是一些改进你的解决方案
function stringWordNumberCount($text){
if (!$text) {
return 0;
}
//Clean the text to remove special character
$text = trim(preg_replace('/[^A-Za-z0-9\-]/', ' ', $text));
//Remove continus space on text
$text = trim( preg_replace('/\s+/', ' ',$text));
//count space
return count(explode(' ', $text));
}
ans:答:
function limit_text($text, $limit) {
if(str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.