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? :/ Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 I love you. Quote Link to comment 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.