ngreenwood6 Posted May 5, 2009 Share Posted May 5, 2009 I have been able to successfully find a word in a string that I have by using strpos(). strpos($variable); However I would like to remove it from the string but I do not know how to do that. Any help is appreciated. Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/ Share on other sites More sharing options...
JonnoTheDev Posted May 5, 2009 Share Posted May 5, 2009 str_replace($variable, "", $string); Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826641 Share on other sites More sharing options...
ngreenwood6 Posted May 5, 2009 Author Share Posted May 5, 2009 I tried that but it doesnt seem to be working. Do I need to assign the variable the value of str_replace like this: $string = "This is a test"; $string = str_replace($string, "", "test"); Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826653 Share on other sites More sharing options...
ngreenwood6 Posted May 5, 2009 Author Share Posted May 5, 2009 Also what about if I want to replace more than one word? Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826654 Share on other sites More sharing options...
nrg_alpha Posted May 5, 2009 Share Posted May 5, 2009 I tried that but it doesnt seem to be working. Do I need to assign the variable the value of str_replace like this: $string = "This is a test"; $string = str_replace($string, "", "test"); You got the argument order wrong: $string = "This is a test"; $string = str_replace('test', '', $string); EDIT - On that note, if your IDE has code hint / completion capabilities, you can always make use of that in the event you are unsure of which order what goes into... Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826656 Share on other sites More sharing options...
ngreenwood6 Posted May 5, 2009 Author Share Posted May 5, 2009 Yeah I figured that out but the way that neil.johnson put it was backwards in my thinking lol. What about if I have 2 words I want replaced, how would I do that? Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826667 Share on other sites More sharing options...
nrg_alpha Posted May 5, 2009 Share Posted May 5, 2009 Use an array: $string = 'This is a test'; $replace = array('This','test'); $string = str_replace($replace, '', $string); Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826675 Share on other sites More sharing options...
ngreenwood6 Posted May 5, 2009 Author Share Posted May 5, 2009 Ok thanks alot. I wasnt sure if you could use an array for it or not. Thanks for the help both of you. Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826677 Share on other sites More sharing options...
ngreenwood6 Posted May 5, 2009 Author Share Posted May 5, 2009 Does str_replace not do "," ? I tried removing the word plus comma like: $variable = "This is a test, thing"; $variable = str_replace("test,", "", $variable); But it doesnt work? Any help? Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826829 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 It works with ",". echo $variable; Link to comment https://forums.phpfreaks.com/topic/156934-remove-from-string/#findComment-826838 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.