4

I've a string from an HL7 message (lab results) and need to add a line break
after each 12 digit number.

Example string:

1    NM    2951-2  S Sodium:  LN     144    mmol/L  mmol/L    135-145       F      201402150533      2    NM    2823-3  S Potassium:  LN     5.6    mmol/L  mmol/L    3.5-5.5    H      F      201402150533      3    NM    2075-0  S Chloride:  LN     103    mmol/L  mmol/L    95-110       F      201402150533

As the 12 digit string is a date and timestamp this will change every test.

flx
  • 14,146
  • 11
  • 55
  • 70
Legless
  • 55
  • 3
  • You can always use regex. – jhnferraris Feb 23 '14 at 05:59
  • Don't you simply mean "How to change newlines to
    elements?" - if this is so, I suspect the *issue* is actually in *how the data is rendered* (e.g. it's directly thrown in HTML helter skelter). I don't see anything "special" about a 12-digit number, except that it is the end of a line.
    – user2864740 Feb 23 '14 at 06:04
  • This looks like you put just the content of OBX fields in one long line. Why did you not add the line break after each OBX segment? – sqlab Feb 24 '14 at 08:46

1 Answers1

6

That is a good time to use a regular expression (see preg_replace):

$str = preg_replace('/\d{12}/', '$0<br>', $str);

replaces each string of 12 numerical digits, with that same string followed by <br>.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • That is perfect. Worked like a charm. Thank you so much. Been banging my head against a wall trying to get that to work. – Legless Feb 23 '14 at 06:03