Linkifies package tracking numbers for DHL, FedEx, Royal Mail (UK), UPS, and USPS.
composer require philipnewcomer/linkify-tracking
First instantiate an instance of the library:
$linkifyTracking = new PhilipNewcomer\LinkifyTracking\LinkifyTracking;
$linkifyTracking->getLinkUrl('12345678901234567890');
Result:
https://tools.usps.com/go/TrackConfirmAction?tLabels=12345678901234567890
$linkifyTracking->getLinkData('12345678901234567890');
Result:
[
'carrier' => 'USPS',
'url' => 'https://tools.usps.com/go/TrackConfirmAction?tLabels=12345678901234567890'
]
$content = '
Here is a tracking number: 12345678901234567890
And another tracking number: 12345678901234567890
';
$linkifyTracking->linkify($content)
Result:
Here is a tracking number: <a href="https://tools.usps.com/go/TrackConfirmAction?tLabels=12345678901234567890">12345678901234567890</a>
And another tracking number: <a href="https://tools.usps.com/go/TrackConfirmAction?tLabels=12345678901234567890">12345678901234567890</a>
Configuration arguments may be passed to the LinkifyTracking
constructor.
The following arguments may be provided:
linkAttributes
: An array of attributes which should be added to the generated HTML links
Example:
$linkifyTracking = new PhilipNewcomer\LinkifyTracking\LinkifyTracking([
'linkAttributes' => [
'class' => 'tracking-link',
'target' => '_blank'
]
]);
Tracking number regular expressions for DHL, FedEx, UPS, and USPS are taken from https://github.com/darkain/php-tracking-urls.