如何计算PHP中特定字符串中的单词?

时间:2021-07-29 15:30:05

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 .

我想计算特定字符串中的单词,因此我可以对其进行验证并阻止用户编写超过100个单词。

I wrote this function but I don't think it's effective enough , I used the explode function with space as a delimiter but what if the user puts two spaces instead of one . can you give me a better way to do that ?

我写了这个函数,但是我认为它不够有效,我使用了带空格的爆炸函数作为分隔符但是如果用户放置两个空格而不是一个空格怎么办。你能给我一个更好的方法吗?

function isValidLength($text , $length){

   $text  = explode(" " , $text );
   if(count($text) > $length)
          return false;
   else
          return true;
}

9 个解决方案

#1


18  

Maybe str_word_count could help

也许str_word_count可以提供帮助

http://php.net/manual/en/function.str-word-count.php

http://php.net/manual/en/function.str-word-count.php

$Tag  = 'My Name is Gaurav'; 
$word = str_word_count($Tag);
echo $word;

#2


10  

You can use the built in PHP function str_word_count. Use it like this:

您可以使用内置的PHP函数str_word_count。像这样用它:

$str = "This is my simple string.";
echo str_word_count($str);

This will output 5.

这将输出5。

If you plan on using special characters in any of your words, you can supply any extra characters as the third parameter.

如果您计划在任何单词中使用特殊字符,则可以提供任何额外字符作为第三个参数。

$str = "This weather is like el ninã.";
echo str_word_count($str, 0, 'àáã');

This will output 6.

这将输出6。

#3


8  

Try this:

尝试这个:

function get_num_of_words($string) {
    $string = preg_replace('/\s+/', ' ', trim($string));
    $words = explode(" ", $string);
    return count($words);
}

$str = "Lorem ipsum dolor sit amet";
echo get_num_of_words($str);

This will output: 5

这将输出:5

#4


4  

This function uses a simple regex to split the input $text on any non-letter character:

此函数使用一个简单的正则表达式来分割任何非字母字符上的输入$ text:

function isValidLength($text, $length) {
    $words = preg_split('#\PL+#u', $text, -1, PREG_SPLIT_NO_EMPTY);
    return count($words) <= $length;
}

This ensures that is works correctly with words separated by multiple spaces or any other non-letter character. It also handles unicode (e.g. accented letters) correctly.

这可确保使用由多个空格或任何其他非字母字符分隔的单词正常工作。它还正确处理unicode(例如重音字母)。

The function returns true when the word count is less than $length.

当字数小于$ length时,该函数返回true。

#5


4  

str_count_words has his flaws. it will count underscores as separated words like this_is two words:

str_count_words有他的缺点。它将下划线计为像this_这两个单词的分隔词:

You can use the next function to count words separated by spaces even if theres more than one between them.

您可以使用下一个函数来计算由空格分隔的单词,即使它们之间有多个单词也是如此。

function count_words($str){

    while (substr_count($str, "  ")>0){
        $str = str_replace("  ", " ", $str);
    }
    return substr_count($str, " ")+1;
}


$str = "This   is  a sample_test";

echo $str;
echo count_words($str);
//This will return 4 words;

#6


2  

Use preg_split() instead of explode(). Split supports regular expressions.

使用preg_split()而不是explode()。 Split支持正则表达式。

#7


1  

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)

使用substr_count计算任何子字符串出现次数。查找单词数量设置为$ 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

#8


0  

There are n-1 spaces between n objects so there will be 99 spaces between 100 words, so u can choose and average length for a word say for example 10 characters, then multiply by 100(for 100 words) then add 99(spaces) then you can instead make the limitation based on number of characters(1099).

n个对象之间有n-1个空格,所以100个单词之间会有99个空格,所以你可以选择一个单词的平均长度,例如10个字符,然后乘以100(100个单词),然后加99(空格)然后你可以改为根据字符数进行限制(1099)。

function isValidLength($text){

if(strlen($text) > 1099)

if(strlen($ text)> 1099)

     return false;

else return true;

否则返回true;

}

}

#9


0  

I wrote a function which is better than str_word_count because that PHP function counts dashes and other characters as words.

我编写了一个比str_word_count更好的函数,因为PHP函数将破折号和其他字符计为单词。

Also my function addresses the issue of double spaces, which many of the functions other people have written don't take account for.

此外,我的函数解决了双空格的问题,其他人编写的许多函数都没有考虑到。

As well this function handles HTML tags. Where if you had two tags nested together and simply used the strip_tags function this would be counted as one word when it's two. For example: <h1>Title</h1>Text or <h1>Title</h1><p>Text</p>

此功能也处理HTML标记。如果你有两个嵌套在一起的标签,并且只使用了strip_tags函数,当它是两个时,这将被算作一个单词。例如:

标题 文字或

标题

文字

Additionally, I strip out JavaScript first other wise the code within the <script> tags would be counted as words.

另外,我首先剥离了JavaScript,

Lastly, my function handles spaces at the beginning and end of a string, multiple spaces, and line breaks, return characters, and tab characters.

最后,我的函数处理字符串开头和结尾的空格,多个空格和换行符,返回字符和制表符。

###############
# Count Words #
###############
function count_words($str)
{
 $str = preg_replace("/[^A-Za-z0-9 ]/","",strip_tags(str_replace('<',' <',str_replace('>','> ',str_replace(array("\n","\r","\t"),' ',preg_replace('~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is','',$str))))));
 while(substr_count($str,'  ')>0)
 {
  $str = str_replace('  ',' ',$str);
 }
 return substr_count(trim($str,' '),' ')+1;
}

#1


18  

Maybe str_word_count could help

也许str_word_count可以提供帮助

http://php.net/manual/en/function.str-word-count.php

http://php.net/manual/en/function.str-word-count.php

$Tag  = 'My Name is Gaurav'; 
$word = str_word_count($Tag);
echo $word;

#2


10  

You can use the built in PHP function str_word_count. Use it like this:

您可以使用内置的PHP函数str_word_count。像这样用它:

$str = "This is my simple string.";
echo str_word_count($str);

This will output 5.

这将输出5。

If you plan on using special characters in any of your words, you can supply any extra characters as the third parameter.

如果您计划在任何单词中使用特殊字符,则可以提供任何额外字符作为第三个参数。

$str = "This weather is like el ninã.";
echo str_word_count($str, 0, 'àáã');

This will output 6.

这将输出6。

#3


8  

Try this:

尝试这个:

function get_num_of_words($string) {
    $string = preg_replace('/\s+/', ' ', trim($string));
    $words = explode(" ", $string);
    return count($words);
}

$str = "Lorem ipsum dolor sit amet";
echo get_num_of_words($str);

This will output: 5

这将输出:5

#4


4  

This function uses a simple regex to split the input $text on any non-letter character:

此函数使用一个简单的正则表达式来分割任何非字母字符上的输入$ text:

function isValidLength($text, $length) {
    $words = preg_split('#\PL+#u', $text, -1, PREG_SPLIT_NO_EMPTY);
    return count($words) <= $length;
}

This ensures that is works correctly with words separated by multiple spaces or any other non-letter character. It also handles unicode (e.g. accented letters) correctly.

这可确保使用由多个空格或任何其他非字母字符分隔的单词正常工作。它还正确处理unicode(例如重音字母)。

The function returns true when the word count is less than $length.

当字数小于$ length时,该函数返回true。

#5


4  

str_count_words has his flaws. it will count underscores as separated words like this_is two words:

str_count_words有他的缺点。它将下划线计为像this_这两个单词的分隔词:

You can use the next function to count words separated by spaces even if theres more than one between them.

您可以使用下一个函数来计算由空格分隔的单词,即使它们之间有多个单词也是如此。

function count_words($str){

    while (substr_count($str, "  ")>0){
        $str = str_replace("  ", " ", $str);
    }
    return substr_count($str, " ")+1;
}


$str = "This   is  a sample_test";

echo $str;
echo count_words($str);
//This will return 4 words;

#6


2  

Use preg_split() instead of explode(). Split supports regular expressions.

使用preg_split()而不是explode()。 Split支持正则表达式。

#7


1  

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)

使用substr_count计算任何子字符串出现次数。查找单词数量设置为$ 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

#8


0  

There are n-1 spaces between n objects so there will be 99 spaces between 100 words, so u can choose and average length for a word say for example 10 characters, then multiply by 100(for 100 words) then add 99(spaces) then you can instead make the limitation based on number of characters(1099).

n个对象之间有n-1个空格,所以100个单词之间会有99个空格,所以你可以选择一个单词的平均长度,例如10个字符,然后乘以100(100个单词),然后加99(空格)然后你可以改为根据字符数进行限制(1099)。

function isValidLength($text){

if(strlen($text) > 1099)

if(strlen($ text)> 1099)

     return false;

else return true;

否则返回true;

}

}

#9


0  

I wrote a function which is better than str_word_count because that PHP function counts dashes and other characters as words.

我编写了一个比str_word_count更好的函数,因为PHP函数将破折号和其他字符计为单词。

Also my function addresses the issue of double spaces, which many of the functions other people have written don't take account for.

此外,我的函数解决了双空格的问题,其他人编写的许多函数都没有考虑到。

As well this function handles HTML tags. Where if you had two tags nested together and simply used the strip_tags function this would be counted as one word when it's two. For example: <h1>Title</h1>Text or <h1>Title</h1><p>Text</p>

此功能也处理HTML标记。如果你有两个嵌套在一起的标签,并且只使用了strip_tags函数,当它是两个时,这将被算作一个单词。例如:

标题 文字或

标题

文字

Additionally, I strip out JavaScript first other wise the code within the <script> tags would be counted as words.

另外,我首先剥离了JavaScript,

Lastly, my function handles spaces at the beginning and end of a string, multiple spaces, and line breaks, return characters, and tab characters.

最后,我的函数处理字符串开头和结尾的空格,多个空格和换行符,返回字符和制表符。

###############
# Count Words #
###############
function count_words($str)
{
 $str = preg_replace("/[^A-Za-z0-9 ]/","",strip_tags(str_replace('<',' <',str_replace('>','> ',str_replace(array("\n","\r","\t"),' ',preg_replace('~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is','',$str))))));
 while(substr_count($str,'  ')>0)
 {
  $str = str_replace('  ',' ',$str);
 }
 return substr_count(trim($str,' '),' ')+1;
}