PHP: str_word_count(åäöåäöåäöåäö) returns the integer value of 12

Related searches

I am using special symbols such as å ä ö on my website which measures the lengths of different texts. Thing is, I have noticed that PHP counts the symbols "å" "ä" "ö" as 1 word each. So åäö counts as 3 words, and åäöåäöåäöåäöåäö counts as 15 words. Well this is clearly not correct and I cannot find an answer to this problem anywhere. I'd be thankful for a useful answer, thank you!

If there's a limited set of word characters that you need to take into account, just supply those into str_word_count with its third param (charlist):

$charlist = 'åäö';
echo str_word_count('åäöåäöåäöåäöåäö', 0, $charlist); // 1

Alternatively, you can write your own Unicode-ready str_word_count function. One possible approach is splitting the source string by non-word symbols, then counting the resulting array:

function mb_str_word_count($str) {
  return preg_match_all('#[\p{L}\p{N}][\p{L}\p{N}\'-]*#u', $str);

Basically, this function counts all the substrings in the target string that start with either Letter or Number character, followed by any number (incl. zero) of Letters, Numbers, hyphens and single quote symbols (matching the description given in str_word_count() docs).

PHP str_word_count() Function, Example. Return an array with the words from the string: <?php print_r( str_word_count("Hello world!"� The str_word_count () function counts the number of words in a string.

You can try adding

setlocale(LC_ALL, 'en_US.utf8')

before your call to str_word_count or roll on your own with

substr_count(trim($str), ' ');

PHP, PHP | str_word_count() Function � $string:This parameter specifies the string whose words the user intends to count. � $returnVal:The return value� The str_word_count () function is a built-in function in PHP and is used to return information about words used in a string like total number word in the string, positions of the words in the string etc.

this work for me... hope its usefull.

USING str_word_count you need to use utf8_decode(utf8_encode)..

function cortar($str)
    if (20>$count=str_word_count($str)) {
        return $str;
        $array = str_word_count($str,1,'.,-0123456789()+=?¿!"<>*ñÑáéíóúÁÉÍÓÚ@|/%$#¡');
        foreach ($array as $e) {
            if (20>$c) {
                if (19>$c) {
                $s.=$e.' ';
        return utf8_decode(utf8_encode($s));

function returs 20 words

PHP str_word_count() function, Description. The str_word_count() function is used to count the number of words in a string. Version: (PHP 4 and above). Syntax: str_word_count(� The str_word_count() function is used to count the number of words in a string. Version: (PHP 4 and above) Syntax: str_word_count(string_name, nformat, add_chars) Parameter:

If it is a string without linebreaks, and words are separated by a whitespace, a simple workaround would be to trim() the string and then count the whitespaces.

$string = "Wörk has to be done.";

// 1 space is 2 words, 2 spaces are 3 words etc.
if(substr_count(trim($string), ' ') > 2)
   // more than 3 words
   // ...

str_word_count() function in PHP, The str_word_count() function returns information about words used in a string. Syntaxstr_word_count(str, return, char)Parametersstr − The� PHP str_word_count () function is used to get the information about the words used in a given input string. It returns the information like total number of words present in the string, positions of the words in the string.

Get String Length, Word Count, and Substring Count in PHP, We will use the following string to demonstrate PHP's strlen , str_word_count , and substr_count functions: // example string $str = 'The letters PHP stand for PHP:� The str_word_count () function in PHP. The php str_word_count () sting function is used to count a number of words in a string. str_word_count () function syntax.

str_word_count. (PHP 4 >= 4.3.0, PHP 5) str_word_count— Return information about words used in a string. Beskrivelse. mixedstr_word_count( string$string[, int$format= 0[, string$charlist]] ) Counts the number of words inside string. If the optional formatis not specified, then the return value will be an integer representing the number of words found.

Get String Length, Word Count, Substring Count PHP provides functions that count how many characters or words are contained in a string or how many times a particular substring occurs in a string. We will use the following string to demonstrate PHP's strlen, str_word_count, and substr_count functions:

  • str_word_count is only intended for ASCII strings. Umlauts require Latin-1 or UTF-8 support.
  • For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters.... what have you set your locale as?
  • Mark Baker, I don't know, how do I check that?
  • Mario, ok.. So is there any solution to this problem?
  • you could probably do some conversion with preg_replace before measuring to get around this
  • Yes thanks for the answer but it only works in that specific case, when I try to count the words in a normal text it returns to normal. Here is a example of a sentence: "Den första november i år, för 23 dagar sedan tackade hon alla som gett henne support och alla hennes vänner."
  • $count = preg_match_all('~\w+~u', $str) might be simpler.
  • It will also take _ into account.
  • Hmm, sorry but could you specify that? I don't know much about Regex :/
  • @georg On the second thought, it's indeed easier to cover all the special cases with preg_match_all.