I know this is an old thread, but I was working on a PHP class to work with HL7.
I wonder what features people will want.
My class allows me to split the HL7v2.x message into a multidimensional array.
I am working on some common things, such as getting the patient’s full name and finding the location of the string.
, . MSH.2, .
.
function parsemsg($string) {
$segs = explode("\n",$string);
$out = array();
if (substr($segs[0],0,3) != 'MSH') {
$out['ERROR'][0] = 'Invalid HL7 Message.';
$out['ERROR'][1] = 'Must start with MSH';
return $out;
exit;
}
$delbarpos = strpos($segs[0],'|',4);
$delchar = substr($segs[0],4,($delbarpos - 4));
define('FLD_SEP', substr($delchar,0,1));
define('SFLD_SEP', substr($delchar,1,1));
define('REP_SEP', substr($delchar,2,1));
define('ESC_CHAR', substr($delchar,3,1));
foreach($segs as $fseg) {
$segments = explode('|',$fseg);
$segname = $segments[0];
$i = 0;
foreach ($segments as $seg) {
if (strpos($seg,FLD_SEP) == false) {
$out[$segname][$i] = $seg;
} else {
$j=0;
$sf = explode(FLD_SEP,$seg);
foreach($sf as $f) {
$out[$segname][$i][$j] = $f;
$j++;
}
}
$i++;
}
}
return $out;
}