Jump to content

[SOLVED] Replacing Characters in an Array a Specified Amount of Times


CloudSex13

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.