3

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!!!!

4
  • 2
    Do you mean 2 consecutive letters or two /same/ letters (like AA,ABB) Commented Jun 13, 2011 at 19:22
  • to consecutive letters like in the above string there are AB RT CD etc... end result would be 1234:21:3AB,3459435:2343RT,23432523:CD, new line would be after comma (sorry about that) Commented Jun 13, 2011 at 19:23
  • 1
    It's missing some details. What's with the "multiple lines"? Can you edit and post the expected output, so we don't have to guess? Commented Jun 13, 2011 at 19:24
  • end result would be 1234:21:3AB,3459435:2343RT,23432523:CD, Commented Jun 13, 2011 at 19:25

7 Answers 7

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

can fourth position from back to three in length be stripped at the same time or i will have to use another regexp so for example get 21:, 234, 523 ?
@jpp: I would do that in a separate regex. Or better yet, you could substr to get the range of characters you want.
but substr from back of the string is possible?
@jpp: Yup. Have a look at the docs. You can use negative numbers to start from (and omit from) the end of the string. So, to get the characters you want you'd do substr($str, -7, 3).
2
$myLine = "1234:21:3AB3459435:2343RT23432523:CD";
$myLine = preg_replace('/([a-z]{2})/i', '$1,', $myLine);

Comments

1

I don't know where you want the new-lines, but as far as the consecutive letters go, it would be something like:

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

Comments

1

Something like this should work for you:

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

Comments

1

try this:

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

Comments

0

Add {2} to make it match exactly twice.

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

Also, you can use \w for any word character.

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

Comments

0

I'm having a bit of trouble interpreting your question. Assuming you mean you'd like your sample data of "1234:21:3AB3459435:2343RT23432523:CD" to be converted to "1234:21:3AB,3459435:2343RT,23432523:CD":

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

should work. The pattern matches exactly two letter characters in a row, and the parentheses around it make that match accessible as a reference in the replacement. The replacement then takes the two matched characters and just puts a comma after them. See http://us.php.net/manual/en/function.preg-replace.php for more details.

If you want the result to be multiple lines (e.g. for prettier output) just change the replacement expression to include a new line, e.g.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.