cjackson111 Posted November 11, 2011 Share Posted November 11, 2011 Hello. I am pulling data from a mysql table and want to replace some strings that may appear in the data. Is it possible to use str_replace to replace more than one value at a time? For instance, replace '+' with 'plus' AND '-' with 'minus'. This doesn't work but something like this -- str_replace($search1, $replace1, $subject1, $search2, $replace2, $subject2); Thanks for all help! Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/ Share on other sites More sharing options...
RaythMistwalker Posted November 11, 2011 Share Posted November 11, 2011 You can use an array: $search = array ('item1' , 'item2') $replace = array('replace1' , 'replace2') str_replace($search, $replace, $subject) If you have a second subject, just redo the str_replace line with $subject2 instead Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287251 Share on other sites More sharing options...
cjackson111 Posted November 11, 2011 Author Share Posted November 11, 2011 Cool, thanks alot. I'll try it in the morning...too sleepy now Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287252 Share on other sites More sharing options...
cypher86 Posted November 11, 2011 Share Posted November 11, 2011 You can use an array: $search = array ('item1' , 'item2') $replace = array('replace1' , 'replace2') str_replace($search, $replace, $subject) If you have a second subject, just redo the str_replace line with $subject2 instead another solution (if you have not that many replace to do) you cando something like: str_replace($search, $replace, str_replace($search, $replace, $subject)) innesting any times you want Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287279 Share on other sites More sharing options...
RaythMistwalker Posted November 11, 2011 Share Posted November 11, 2011 another solution (if you have not that many replace to do) you cando something like: str_replace($search, $replace, str_replace($search, $replace, $subject)) innesting any times you want This would just result in a very messy code even if it does work. but I don't see why you have str_replace() inside str_replace() Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287324 Share on other sites More sharing options...
cypher86 Posted November 11, 2011 Share Posted November 11, 2011 in case you have to replace different things...... anyway you're right i should have wrote: str_replace($search1, $replace1, str_replace($search2, $replace2, $subject)) Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287326 Share on other sites More sharing options...
silkfire Posted November 11, 2011 Share Posted November 11, 2011 If your replacement strings are similar you can use strstr. Although slower, it will prevent overwriting of previous replacements as it only replaces each replacement string ones and "locks" it for writing (token). Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287338 Share on other sites More sharing options...
xyph Posted November 11, 2011 Share Posted November 11, 2011 If your replacement strings are similar you can use strstr. Although slower, it will prevent overwriting of previous replacements as it only replaces each replacement string ones and "locks" it for writing (token). strstr doesn't directly replace. Disregard the above post. Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287377 Share on other sites More sharing options...
silkfire Posted November 11, 2011 Share Posted November 11, 2011 I meant strtr(). Disregard the above post. Quote Link to comment https://forums.phpfreaks.com/topic/250911-multiple-str_replace/#findComment-1287381 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.