I have been trying to figure out how to convert string below string to multiple lines where it will add comma after two consecutive letters. Anyhelp is appreciated.
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace('/((?<=\[a-zA-Z]\b))/', ',', $myLine);
output would be
1234:21:3AB,
3459435:2343RT,
23432523:CD,
THanks, jp
I like all the answers, i appreciate everyone pitching in to help and ran through all the various different ways of getting this to work. it is amazing what regexp php can do one thing so many different ways. thanks to all again!!!!
Here's something I came up with quickly.
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace('/([a-zA-Z]{2})/', "$1,\n", $myLine);
Outputs:
1234:21:3AB,
3459435:2343RT,
23432523:CD,
Or, if you don't want the trailing comma:
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace('/([a-zA-Z]{2}(?!$))/', "$1,\n", $myLine);
Outputs:
1234:21:3AB,
3459435:2343RT,
23432523:CD
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine = preg_replace('/([a-z]{2})/i', '$1,', $myLine);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With