Jump to content

Archived

This topic is now archived and is closed to further replies.

atitthaker

Problem with str_word_count()

Recommended Posts

I am having little problem with understanding behaviour of str_word_count().

When I am using "--" in my string in the middle of a word, it breaks word in between and does not include"--" in the counting.

Code:

//counting number of words in the string "This is sample string and th--is is another", with getting the words option...

print_r(str_word_count("This is sample string and th--is is another",1));

and the output I get is:

Array
(
    [0] => This
    [1] => is
    [2] => sample
    [3] => string
    [4] => and
    [5] => th
    [6] => is
    [7] => is
    [8] => another
)

As u can see, the word "th--is" is broken and "--" is not taken under the consideration...
I am using PHP 5.0.2.

Plz help...
Thanks

Share this post


Link to post
Share on other sites
from the manual:

[quote]
For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters.
[/quote]

Share this post


Link to post
Share on other sites
I would do:

[code]<?php

$string = "This is sample string and th--is is another";
$string_formatted = str_replace("--", "", $string);

print_r(str_word_count($string_formatted));
?>[/code]

That's if I understood your question and needs properly.

Share this post


Link to post
Share on other sites
just out of curiosity, why are you using -- in the middle of your strings?  I'm looking at your example string and it SEEMS to me that you are using -- as a seperator for multiple values, that you would maybe later on explode the string at the -- to form an array? I may be way off here, but is that what it's for?

Share this post


Link to post
Share on other sites
Thanks for the reply...
I am a new learner of PHP and I was just trying different things. It's is true that it's meaningless to put "--" in the middle of the string, but I was just trying the thing. As it works with "-" I thought it would work with "--" too. but it seems it is just for "-" only and not for "--".

Thanks once again.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.