PHPackages                             axeesante/hl7 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. axeesante/hl7

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

axeesante/hl7
=============

HL7 parser, generator and sender.

1.9.4(4y ago)11.8k↓87.5%PHPPHP &gt;=7.2

Since Nov 16Pushed 4y agoCompare

[ Source](https://github.com/leaaxepartner/HL7)[ Packagist](https://packagist.org/packages/axeesante/hl7)[ RSS](/packages/axeesante-hl7/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (5)Dependencies (2)Versions (37)Used By (0)

[![Build Status](https://camo.githubusercontent.com/b0f871e3da3a20d4baaecd3c37ea9e530ba6e1d463195e5676c2cfc1e319bc55/68747470733a2f2f7472617669732d63692e6f72672f73656e6172616e79612f484c372e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/senaranya/HL7)[![Total Downloads](https://camo.githubusercontent.com/0f30d9a56290153508243c37c550bb240c1f222b719cf5e743c01d4555dee37c/68747470733a2f2f706f7365722e707567782e6f72672f6172616e796173656e2f686c372f646f776e6c6f616473)](https://packagist.org/packages/aranyasen/hl7)[![Latest Stable Version](https://camo.githubusercontent.com/c9b8d80112dc9b469a64eec491d6595c2c50e664b93f165d1eea99c3027aabe2/68747470733a2f2f706f7365722e707567782e6f72672f6172616e796173656e2f686c372f762f737461626c65)](https://packagist.org/packages/aranyasen/hl7)[![License](https://camo.githubusercontent.com/503727685e2e123d8e9efddd52e45dc150fb45c347ac7f737f4e712c5a340d2f/68747470733a2f2f706f7365722e707567782e6f72672f6172616e796173656e2f686c372f6c6963656e7365)](https://packagist.org/packages/aranyasen/hl7)

**Important: Supported PHP version has been updated to 7.2+. To use this package with 7.0 or 7.1, use previous release [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)****Important: Exception message for invalid segment name changed to "Segment name '$name' should be 3 characters and in uppercase"**

Introduction
------------

[](#introduction)

A PHP-based HL7 v2.x Parsing, Generation and Sending library, inspired from the famous Perl Net-HL7 package.

Installation
------------

[](#installation)

```
composer require aranyasen/hl7
```

Usage
-----

[](#usage)

### Parsing

[](#parsing)

```
// Create a Message object from a HL7 string
$msg = new Message("MSH|^~\\&|1|\rPID|||abcd|\r"); // Either \n or \r can be used as segment endings
$pid = $msg->getSegmentByIndex(1);
echo $pid->getField(3); // prints 'abcd'
echo $msg->toString(true); // Prints entire HL7 string

// Get the first segment
$msg->getFirstSegmentInstance('PID'); // Returns the first PID segment. Same as $msg->getSegmentsByName('PID')[0];

// Check if a segment is present in the message object
$msg->hasSegment('PID'); // return true or false based on whether PID is present in the $msg object

// Check if a message is empty
$msg = new Message();
$msg->isempty(); // Returns true
```

### Creating new messages

[](#creating-new-messages)

```
// Create an empty Message object, and populate MSH and PID segments...
$msg = new Message();
$msh = new MSH();
$msg->addSegment($msh); // Message is: "MSH|^~\&|||||20171116140058|||2017111614005840157||2.3|\n"

// Create any custom segment
$abc = new Segment('ABC');
$abc->setField(1, 'xyz');
$abc->setField(4, ['']); // Set an empty field at 4th position. 2nd and 3rd positions will be automatically set to empty
$msg->setSegment($abc, 1); // Message is now: "MSH|^~\&|||||20171116140058|||2017111614005840157||2.3|\nABC|xyz|\n"

// Create a defined segment (To know which segments are defined in this package, look into Segments/ directory)
// Advantages of defined segments over custom ones (shown above) are 1) Helpful setter methods, 2) Auto-incrementing segment index
$pid = new PID(); // Automatically creates PID segment, and adds segment index at PID.1
$pid->setPatientName([$lastname, $firstname, $middlename, $suffix]); // Use a setter method to add patient's name at standard position (PID.5)
$pid->setField('abcd', 5); // Apart from standard setter methods, you can manually set a value at any position too
unset $pid; // Destroy the segment and decrement the id number. Useful when you want to discard a segment.
```

```
// Creating multiple message objects may have an unexpected side-effect: segments start with wrong index values (Check tests/MessageTest for explanation)...
// Use 4th argument as true, or call resetSegmentIndices() on $msg object to reset segment indices to 1
$msg = new Message("MSH|^~\&|||||||ORM^O01||P|2.3.1|", null, true, true);
// ... any segments added here will now start index from 1, as expected.
```

```
// Sometimes you may want to have exact index values, rather than auto-incrementing for each instance of a segment
// Use 5th argument as false...
$hl7String = "MSH|^~\&|||||||ORU^R01|00001|P|2.3.1|\n" . "OBX|1||11^AA|\n" . "OBX|1||22^BB|\n";
$msg = new Message($hl7String, null, true, true, false); // $msg contains both OBXs with given indexes in the string
```

```
// Create a segment with empty sub-fields retained
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", null, true); // Third argument 'true' forces to keep all sub fields
$pv1 = $msg->getSegmentByIndex(1);
$fields = $pv1->getField(3); // $fields is ['', 'AAAA1', '', '', 'BB']

// Create/send message with segment-ending bar character (|) removed
$msg = new Message("MSH|^~\\&|1|\nABC|||xxx\n", ['SEGMENT_ENDING_BAR' => false]);
$msg->toString(true); // Returns "MSH|^~\&|1\nABC|||xxx\n"
(new Connection($ip, $port))->send($msg); // Sends the message without ending bar-characters (details on Connection below)

// Specify custom values for separators, HL7 version etc.
$msg = new Message("MSH|^~\\&|1|\rPV1|1|O|^AAAA1^^^BB|", ['SEGMENT_SEPARATOR' => '\r\n', 'HL7_VERSION' => '2.3']);
```

### Send messages to remote listeners

[](#send-messages-to-remote-listeners)

```
$ip = '127.0.0.1'; // An IP
$port = '12001'; // And Port where a HL7 listener is listening
$message = new Message($hl7String); // Create a Message object from your HL7 string

// Create a Socket and get ready to send message. Optionally add timeout in seconds as 3rd argument (default: 10 sec)
$connection = new Connection($ip, $port);
$response = $connection->send($message); // Send to the listener, and get a response back
echo $reponse->toString(true); // Prints ACK from the listener
```

### ACK

[](#ack)

Handle ACK message returned from a remote HL7 listener...

```
$ack = (new Connection($ip, $port))->send($message); // Send a HL7 to remote listener
$returnString = $ack->toString(true);
if (strpos($returnString, 'MSH') === false) {
    echo "Failed to send HL7 to 'IP' => $ip, 'Port' => $port";
}
$msa = $ack->getSegmentsByName('MSA')[0];
$ackCode = $msa->getAcknowledgementCode();
if ($ackCode[1] === 'A') {
    echo "Recieved ACK from remote\n";
}
else {
    echo "Recieved NACK from remote\n";
    echo "Error text: " . $msa->getTextMessage()
}
```

APIs
----

[](#apis)

This package exposes a number of public methods for convenient HL7 handling. Some examples are:

1. Considering you have a Message object (say, `$msg = new Message(file_get_contents('somefile.hl7'));`)

```
    $msg->toFile('/path/to/some.hl7'); // Write to a file
    $msg->isOru(); // Check if it's an ORU
    $msg->isOrm(); // Check if it's an ORM
```

Visit [docs\\README](docs/README.md) for details on available APIs

All segment level getter/setter APIs can be used in two ways -

- If a position index isn't provided as argument (1st argument for getters, 2nd for setters), a standard index is used.
    `$pid->setPatientName('John Doe')` -&gt; Set patient name at position 5 as per HL7 v2.3 [standard](https://corepointhealth.com/resource-center/hl7-resources/hl7-pid-segment)
    `$pid->getPatientAddress()` -&gt; Get patient address from standard 11th position
- To use a custom position index, provide it in the argument:
    `$pid->setPatientName('John Doe', 6)` -&gt; Set patient name at 6th position in PID segment
    `$pid->getPatientAddress(12)` -&gt; Get patient address from 12th position

### TODOs

[](#todos)

- Data Validation
- Search by regex and return segment/field/index
- Define more segments

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 55.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~42 days

Recently: every ~151 days

Total

33

Last Release

1753d ago

PHP version history (2 changes)1.0.0PHP &gt;=7.1

1.6.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/41efc0c08d6c16b7b01ca4576a048a269ad7d978451ce6935c2a4a3d91319808?d=identicon)[leaaxepartner](/maintainers/leaaxepartner)

---

Top Contributors

[![senaranya](https://avatars.githubusercontent.com/u/5471894?v=4)](https://github.com/senaranya "senaranya (33 commits)")[![leaaxepartner](https://avatars.githubusercontent.com/u/49435283?v=4)](https://github.com/leaaxepartner "leaaxepartner (19 commits)")[![isangil](https://avatars.githubusercontent.com/u/403087?v=4)](https://github.com/isangil "isangil (2 commits)")[![ajibarra](https://avatars.githubusercontent.com/u/794722?v=4)](https://github.com/ajibarra "ajibarra (2 commits)")[![mmonahanfl](https://avatars.githubusercontent.com/u/36053511?v=4)](https://github.com/mmonahanfl "mmonahanfl (1 commits)")[![DamienHarper](https://avatars.githubusercontent.com/u/2448660?v=4)](https://github.com/DamienHarper "DamienHarper (1 commits)")[![silvioq](https://avatars.githubusercontent.com/u/155036?v=4)](https://github.com/silvioq "silvioq (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/axeesante-hl7/health.svg)

```
[![Health](https://phpackages.com/badges/axeesante-hl7/health.svg)](https://phpackages.com/packages/axeesante-hl7)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
