php preg_replace每隔一次出现

I need to transform this

329638.798,7387605.001,330131.098,7387324.001,329740.398,7386851.001
                      ^                      ^

Into this

329638.798,7387605.001L330131.098,7387324.001L329740.398,7386851.001
                      ^                      ^

More simply

a1,b1,a2,b2,a3,b3 => a1,b1La2,b2La3,b3

This means replace every second occurence of comma with an 'L'.

I've read this post and tried this

preg_replace('/(\S+,\S+)\s,/', '$1L', $geom);

without success.

In your pattern you have \S - it matches anything that's not a whitespace character, which includes the comma. You can use \w instead. Here's how:

echo preg_replace('/(\w+,.*?\w),/', '$1L', $geom)

Output:

329638.798,7387605.001L330131.098,7387324.001L329740.398,7386851.001

Demo.

If you still want to use regex, use this:

preg_replace('/(,[^,]*?),/', '$1L', $geom)

Quick solution:

$input = 'a1,b1,a2,b2,a3,b3';

echo preg_replace('/([^,]*?,[^,]*?),/', '$1L', $input);

Output:

a1,b1La2,b2La3,b3