Lisa23 Posted November 24, 2011 Share Posted November 24, 2011 hi is there a way of doing a str replace to look twice like at moment i have if find space replace with "-" PHP Code: .str_replace(' ','-',($row['model'])) is there a way i can do if find twice $row['model if find space " " replace with "-" also if find "i30" replace with "10" is there a way i can try find another like if find " " replace with "-" also if find "130" replace with hello world?? thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/251752-hi-is-there-a-way-to-use-str_replace-to-replace-if-find-two-values/ Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2011 Share Posted November 24, 2011 The manual explains how to use arrays to do multiple replacements: str_replace Quote Link to comment https://forums.phpfreaks.com/topic/251752-hi-is-there-a-way-to-use-str_replace-to-replace-if-find-two-values/#findComment-1291018 Share on other sites More sharing options...
Lisa23 Posted November 24, 2011 Author Share Posted November 24, 2011 HYi thanks for the link i found out that i can do it this way // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); but is there a way that i can just implement .str_replace(' ','-','cars','bikes'($row['model'])) is there? Quote Link to comment https://forums.phpfreaks.com/topic/251752-hi-is-there-a-way-to-use-str_replace-to-replace-if-find-two-values/#findComment-1291019 Share on other sites More sharing options...
kicken Posted November 25, 2011 Share Posted November 25, 2011 No, you can't just add arguments. You can inline the array's if you want, but if your number of replacements gets large it's more readable to have them separate. str_replace(array(' ', '-'), array('cars', 'bikes'), $row['model']); Quote Link to comment https://forums.phpfreaks.com/topic/251752-hi-is-there-a-way-to-use-str_replace-to-replace-if-find-two-values/#findComment-1291062 Share on other sites More sharing options...
anups Posted November 25, 2011 Share Posted November 25, 2011 try this $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); echo $onlyconsonants ; Quote Link to comment https://forums.phpfreaks.com/topic/251752-hi-is-there-a-way-to-use-str_replace-to-replace-if-find-two-values/#findComment-1291078 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.