CloudSex13 Posted May 12, 2009 Share Posted May 12, 2009 In example, I'm looking on how to replace the string "5," with an empty string "" a specified amount of times. preg_replace does not work because the "," is in the string. str_replace replaces all matched values, and to my knowledge, you cannot specify a manual amount of replace times for multiple matches. Could anyone give me a pointer? :/ Link to comment https://forums.phpfreaks.com/topic/157782-solved-replacing-characters-in-an-array-a-specified-amount-of-times/ Share on other sites More sharing options...
thebadbad Posted May 12, 2009 Share Posted May 12, 2009 preg_replace() has a limit parameter. And I can't see why it shouldn't work because of the comma?: <?php $str = 'test 5,1 another 5,73 5,01 5,2 5,0 5,5 5,3 test'; $limit = 3; echo preg_replace('~5,~', '', $str, $limit); ?> Output: test 1 another 73 01 5,2 5,0 5,5 5,3 test Link to comment https://forums.phpfreaks.com/topic/157782-solved-replacing-characters-in-an-array-a-specified-amount-of-times/#findComment-832199 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 I love you. Link to comment https://forums.phpfreaks.com/topic/157782-solved-replacing-characters-in-an-array-a-specified-amount-of-times/#findComment-832202 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.