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? Link to comment https://forums.phpfreaks.com/topic/46043-string-help/ Share on other sites More sharing options...
tarun Posted April 7, 2007 Share Posted April 7, 2007 Not Sure But Probably preg_match Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223694 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 Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223705 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? Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223711 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); Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223713 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 Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223714 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...) Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223719 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 "" ? Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223721 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 Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223726 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? Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223737 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? Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223742 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 Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223743 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 />"; ?> Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223774 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 =) Link to comment https://forums.phpfreaks.com/topic/46043-string-help/#findComment-223776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.