how to count the words in a specific string in PHP?

前端 未结 9 1543
忘了有多久
忘了有多久 2020-12-18 21:45

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 .

I wrote this function but I don\'t

相关标签:
9条回答
  • 2020-12-18 22:05

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

    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>

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

    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;
    }
    
    0 讨论(0)
  • 2020-12-18 22:06

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

    0 讨论(0)
  • 2020-12-18 22:07

    Maybe str_word_count could help

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

    $Tag  = 'My Name is Gaurav'; 
    $word = str_word_count($Tag);
    echo $word;
    
    0 讨论(0)
提交回复
热议问题