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? :/

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.