Jump to content


Photo

Problem with str_word_count()


  • Please log in to reply
4 replies to this topic

#1 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 23 August 2006 - 04:46 AM

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
Beginner with PHP and need help

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 August 2006 - 04:53 AM

from the manual:

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.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Jervous

Jervous
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 06:58 AM

I would do:

<?php

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

print_r(str_word_count($string_formatted));
?>

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

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 August 2006 - 07:05 AM

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?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 23 August 2006 - 09:51 AM

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.
Beginner with PHP and need help




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users