Skip to main content
added image
Source Link
Colin O'Dell
  • 8.6k
  • 8
  • 38
  • 75

You can use a "positive lookahead" like this:

,(?=.*,)

The lookahead is the part in parens. It basically says "only replace this comma if there's another comma later in the string.

The code would look like this:

echo preg_replace('/,(?=.*,)/', '', $str);

I tested this with RegexBuddy to confirm it works:

enter image description here

You can use a "positive lookahead" like this:

,(?=.*,)

The lookahead is the part in parens. It basically says "only replace this comma if there's another comma later in the string.

The code would look like this:

echo preg_replace(',(?=.*,)', '', $str);

You can use a "positive lookahead" like this:

,(?=.*,)

The lookahead is the part in parens. It basically says "only replace this comma if there's another comma later in the string.

The code would look like this:

echo preg_replace('/,(?=.*,)/', '', $str);

I tested this with RegexBuddy to confirm it works:

enter image description here

Source Link
Colin O'Dell
  • 8.6k
  • 8
  • 38
  • 75

You can use a "positive lookahead" like this:

,(?=.*,)

The lookahead is the part in parens. It basically says "only replace this comma if there's another comma later in the string.

The code would look like this:

echo preg_replace(',(?=.*,)', '', $str);