e111982 Posted November 22, 2009 Share Posted November 22, 2009 Hi. I have got a problem. I have a string like: $old="apple"; $new="lemon"; $str="I have got one green apple, two yellow apples, oranges and grapes."; I want an output as : I have got one green lemon,two yellow apples, oranges and grapes. However str_replace replaces all occurrences. I need only words to be replaced. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/ Share on other sites More sharing options...
Andy-H Posted November 22, 2009 Share Posted November 22, 2009 Use preg_replace it allows you to add a limit to the replacement. <?php $old ="#apple#i"; $new ="lemon"; $str ="I have got one green apple, two yellow apples, oranges and grapes."; $str = preg_replace($old, $new, $str, 1); echo 'Output: ' . $str; ?> Output: I have got one green lemon, two yellow apples, oranges and grapes. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963275 Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 You can use preg_replace to do this. Example: $old="apple"; $new="lemon"; $str="I have got one green apple, two yellow apples, oranges and grapes."; echo preg_replace("~\b$old\b~", $new, $str); Edit: Angy-H's solution will only work if the occurrence of the string that you want to replace is the first one in the string. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963276 Share on other sites More sharing options...
e111982 Posted November 22, 2009 Author Share Posted November 22, 2009 thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963280 Share on other sites More sharing options...
Andy-H Posted November 22, 2009 Share Posted November 22, 2009 Can you explain what the word boundry is doing? I was looking into it and found this: http://www.regular-expressions.info/wordboundaries.html but it is confusing as hell.... EDIT: NVM, just noticed that the second word is apples and not apple and it hit me, lol. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963287 Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 The word boundary, \b, just matches anything that might border a word. Ex: space, comma, period, etc.. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963289 Share on other sites More sharing options...
Andy-H Posted November 22, 2009 Share Posted November 22, 2009 So the boundary only matches on the condition that the boundary character matched is non-word charter? Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963292 Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 Basically, here is some good reading on word boundaries in regex. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963301 Share on other sites More sharing options...
e111982 Posted November 23, 2009 Author Share Posted November 23, 2009 How can I make it case insensitive? For example: $old="apple"; $new="lemon"; $str="I have got one green Apple, two yellow apples, oranges and grapes."; echo preg_replace("~\b$old\b~", $new, $str); returns: I have got one green Apple, two yellow apples, oranges and grapes. Is there a way for it to return : I have got one green lemon, two yellow apples, oranges and grapes. Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963786 Share on other sites More sharing options...
Mchl Posted November 23, 2009 Share Posted November 23, 2009 ~\b$old\b~i Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-963793 Share on other sites More sharing options...
emopoops Posted November 23, 2009 Share Posted November 23, 2009 u could use array jews asl weel.. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-964367 Share on other sites More sharing options...
Maq Posted November 23, 2009 Share Posted November 23, 2009 u could use array jews asl weel.. Huh...? Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-964369 Share on other sites More sharing options...
emopoops Posted November 23, 2009 Share Posted November 23, 2009 im sorry i meant an array of keys as well. Quote Link to comment https://forums.phpfreaks.com/topic/182508-replace-single-words-in-php/#findComment-964378 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.