Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class HL7Parser
- {
- var $messageElements = array("MSH" => 18, "PID" => 30, "PV1" => 52, "IN1" => 49, "ORC" => 19, "OBR" => 43);
- function getDatabaseInput($hl7message)
- {
- if ($hl7message) {
- // hl7message exists
- $hl7ausgabe = $this->parsemsg($hl7message);
- $selectedInformation = array();
- $selectedInformation["patientID"] = $this->getDatabaseElement($hl7ausgabe, array('PID', 2));
- $selectedInformation["patientLastName"] = $this->getDatabaseElement($hl7ausgabe,array('PID', 4, 0));
- $selectedInformation["patientFirstname"] = $this->getDatabaseElement($hl7ausgabe,array('PID', 4, 1));
- if ($selectedInformation["patientFirstname"] = '' && $selectedInformation["patientLastName"] = '' ){
- $selectedInformation["patientLastName"] = $this->getDatabaseElement($hl7ausgabe,array('PID', 4));
- }
- $selectedInformation["patientCode"] = $this->getDatabaseElement($hl7ausgabe,array('OBR', 3, 0));
- $selectedInformation["patientDateOfBirth"] = $this->parseDateFormat($this->getDatabaseElement($hl7ausgabe,array('PID', 6)));
- $selectedInformation["patientWard"] = "Found in Second IN1 segment 36 at 21012.hl7";
- $selectedInformation["patientCaseNr"] = $this->getDatabaseElement($hl7ausgabe,array('PV1', 19));
- $selectedInformation["patientKlAu-Nr"] = $this->getDatabaseElement($hl7ausgabe,array('ORC', 4));
- $selectedInformation["patientKlAu-Dat"] = $this->parseDateTimeFormat($this->getDatabaseElement($hl7ausgabe,array('OBR', 7)));
- return $selectedInformation;
- }else{
- //no hl7 message found
- return false;
- }
- }
- function getDatabaseElement($array, $segments)
- {
- foreach ($segments as $segment) {
- if (is_array($array) && array_key_exists($segment, $array)) {
- $array = $array[$segment];
- } else {
- return "";
- }
- }
- return $array;
- }
- function parseDateFormat($string)
- {
- if ($string == ''){
- return '';
- }
- $string = substr_replace($string, '-', 4, 0);
- $string = substr_replace($string, '-', 7, 0);
- return $string;
- }
- function parseDateTimeFormat($string)
- {
- $string = utf8_encode($string);
- $string = str_replace(" ", "", $string);
- $string = str_replace("\t", "", $string);
- if ($string == ''){
- return '';
- }
- $date = $this->parseDateFormat(substr($string, 0, 8));
- $time = substr($string, 8);
- $time = substr_replace($time, ':', 4, 0);
- $time = substr_replace($time, ':', 2, 0);
- return $date . " " . $time;
- }
- function parsemsg($string)
- {
- //looks for the closing bar of the delimiting characters
- if (substr($string, 0, 3) != 'MSH') {
- $out['ERROR'][0] = 'Invalid HL7 Message.';
- $out['ERROR'][1] = 'Must start with MSH';
- return $out;
- }
- //get delimiting characters
- $delbarpos = strpos($string, '|', 4);
- $delchar = substr($string, 4, ($delbarpos - 4));
- if (!defined('FLD_SEP')) {
- define('FLD_SEP', substr($delchar, 0, 1));
- }
- if (!defined('SFLD_SEP')) {
- define('SFLD_SEP', substr($delchar, 1, 1));
- }
- if (!defined('REP_SEP')) {
- define('REP_SEP', substr($delchar, 2, 1));
- }
- if (!defined('ESC_CHAR')) {
- define('ESC_CHAR', substr($delchar, 3, 1));
- }
- $out = array();
- while (strlen($string) > 0) {
- $segname = substr($string, 0, strpos($string, '|'));
- if (array_key_exists($segname, $this->messageElements)) {
- //valid message element name
- $string = substr($string, 3);
- $out[$segname] = array();
- for ($elementCount = 0; $elementCount < $this->messageElements[$segname]; $elementCount++) {
- $delbarposCurrent = strpos($string, '|');
- $delbarposNext = strpos($string, '|', $delbarposCurrent + 1);
- if (($elementCount + 1) == $this->messageElements[$segname]) {
- //end of segment, next segment must be manually seperated
- if ($delbarposNext) {
- //next segment exists
- $delbarposNext -= 3;
- } else {
- //no next segment -> end of message
- $delbarposNext = strlen($string);
- }
- }
- $element = substr($string, $delbarposCurrent + 1, $delbarposNext - 1);
- if (strpos($element, FLD_SEP) == false) {
- //no field seperator -> only one information in element
- $out[$segname][] = $element;
- } else {
- //field seperator found -> element contains multiple information
- $elements = array();
- $sf = explode(FLD_SEP, $element);
- foreach ($sf as $f) {
- $elements[] = $f;
- }
- $out[$segname][] = $elements;
- }
- $string = substr($string, $delbarposNext);
- }
- } else {
- $out['ERROR'][0] = 'Invalid HL7 Message.';
- $out['ERROR'][1] = $segname . " is not a valid Segment";
- return $out;
- }
- }
- return $out;
- }
- }
- function getFiles($directory)
- {
- //check directory for new hl7 messages
- $handle = opendir($directory);
- $files = array();
- while ($file = readdir($handle)) {
- if ($file != "." && $file != ".." && !is_dir($file) && $file != 'Description') {
- $files[] = $file;
- }
- }
- return $files;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement