Using regular expressions and php

I tried to figure out how to convert the line under the line to several lines, where it will add a comma after two consecutive letters. Anyhelp is rated.

$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace('/((?<=\[a-zA-Z]\b))/', ',', $myLine);

will be

1234:21:3AB,
3459435:2343RT,
23432523:CD,

thanking JP

I like all the answers, I appreciate everyone who is applying to help and have run through all the different ways to make this work. it is amazing that regexp php can do one thing in so many different ways. thank you all again !!!!

+3
source share
7 answers

Here, I came up with something 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 do not want a comma with a 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
+2
source
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine = preg_replace('/([a-z]{2})/i', '$1,', $myLine);
+2
source

, , , :

$myLine= preg_replace('/([a-zA-Z]{2})/', '$1,', $myLine);
+1

- :

preg_replace('~([a-z]{2})~i', "$1,", $myLine)
+1

:

$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine= preg_replace("/([a-z]{2})/i","$1,\n", $myLine);
+1

{2}, .

/((?<=[a-zA-Z]{2}))/

, \w .

/((?<=\w{2}\b))/
0

. , , "1234: 21: 3AB3459435: 2343RT23432523: CD" "1234: 21: 3AB, 3459435: 2343RT, 23432523: CD":

$myLine= preg_replace('/([a-zA-Z]{2})/','$1,',$myLine);

. , . . . http://us.php.net/manual/en/function.preg-replace.php.

, (, ), , ,

$myLine= preg_replace('/([a-zA-Z]{2})/','$1'.",\n",$myLine);
0

All Articles