Search code examples
phpcsvreplace

Replace a whole integer match in a delimited string and a maximum of one of its adjacent commas


I have this string:

525,294,475,215,365,745

I need to remove 475 ...and its preceding comma.

If I need to remove the first number (and there's more than one number in the string), I need to remove the following comma.

How can I do this? A regular expression?


Solution

  • Here's a regular expression:

    $s = "525,294,475,215,365,745";
    $s = preg_replace(',?475', '', $s);