atitthaker Posted August 23, 2006 Share Posted August 23, 2006 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted August 23, 2006 Share Posted August 23, 2006 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] Quote Link to comment Share on other sites More sharing options...
Jervous Posted August 23, 2006 Share Posted August 23, 2006 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted August 23, 2006 Share Posted August 23, 2006 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? Quote Link to comment Share on other sites More sharing options...
atitthaker Posted August 23, 2006 Author Share Posted August 23, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.