pouncer Posted April 7, 2007 Share Posted April 7, 2007 Say for e.g, i have a sentence: "Hello, my name is Johnny and I like DVD's" How do I extract each word from the sentence and echo it? and also, i want remove any ' from any words, and also if the word ends in s, i want it to remove the s. so with this sentence it shud echo: Hello my name is Johnny and I like DVD can anyone help me achieve this? Quote Link to comment Share on other sites More sharing options...
tarun Posted April 7, 2007 Share Posted April 7, 2007 Not Sure But Probably preg_match Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 7, 2007 Share Posted April 7, 2007 maybe using str_replace and then adding the fixed string to an array? that's what I would have tried first Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 7, 2007 Author Share Posted April 7, 2007 str_replace for what mate? hmm, im not sure how to do it. can anyone give any sample codes? Quote Link to comment Share on other sites More sharing options...
fert Posted April 7, 2007 Share Posted April 7, 2007 $string=str_replace("'","",$string); $string=str_ireplace("'s","",$string); //only works for php 5 $words=explode(" ",$string); print_r($words); Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 7, 2007 Share Posted April 7, 2007 yeah... i was just writing something simular but got this message: Warning - while you were typing a new reply has been posted. You may wish to review your post. but then again fert... he needs to remove that ugly Array ( ) hehe... add this below what fert said: foreach ($words as $word) { echo $word."<br>"; } think that should do it for you Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 7, 2007 Author Share Posted April 7, 2007 is there a way i can put all my replace in 1 function like str_replace("'", "", ".", "", ...... etc...) Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 7, 2007 Author Share Posted April 7, 2007 and also i cant use $string=str_ireplace("'s","",$string); //only works for php 5 because mine isnt php5 any other ways to replace "'s" with "" ? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 7, 2007 Share Posted April 7, 2007 read here http://no.php.net/str_replace Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 7, 2007 Author Share Posted April 7, 2007 thanks clown. ive got this $string = "My favourite DVD's!"; $un_needed = array("!", "'s", "'"); $string = str_replace($un_needed, "", $string); any other characters you can think to replace with blank? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 7, 2007 Share Posted April 7, 2007 dunno... why do you want to replace them anyway? Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 7, 2007 Author Share Posted April 7, 2007 because i send each word to a site to see if its a noun or an adjective Quote Link to comment Share on other sites More sharing options...
per1os Posted April 7, 2007 Share Posted April 7, 2007 http://us2.php.net/manual/en/function.str-word-count.php Does not get much easier than that: <?php $str = "Hello, my name is Johnny and I like DVD's"; $wordArr = str_word_count($str, 1); foreach ($wordArr as $word) print $word . "<br />"; ?> Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 7, 2007 Share Posted April 7, 2007 haha... nice one frost110 =) well glad you're back... i just learned something new to now =) 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.