Shipped 1-95080 via other USPS with tracking 1Z2216FE0348543895.
Shipped 1-95080 via other FedEx with tracking 729870539581, 729870539592.
Here are two separate "comments" or pieces of data that are entered into an individual orders comment data.
I need to search the comment data for the "carrier" and the individual tracking numbers but their formats vary in format depending on the carrier.
We only use 2 shipping carriers USPS and FedEx for package tracking. I want to create a function to extract the carrier type and extract just the tracking numbers from these comments to place them into individual places in our database for future use. I just hate regular expressions.
Does anyone have anything that might point me in the right direction? (Also this is all in PHP)
I read you said that hate regexp, but it could be useful to this case. I wrote an example that could help you.
To the first phrase:
<?php
$string = 'Shipped 1-95080 via other USPS with tracking 1Z2216FE0348543895.';
preg_match('/.+(?P<ship_num>\d{1}\-\d+).+via other (?P<company>\w+) with tracking (?<tracking_code>[\w,\s]+)/', $string, $match);
if(strpos($match['tracking_code'], ',')!==false) {
$match['tracking_code'] = array_map(function($index) {
return trim($index);
}, explode(',', $match['tracking_code']));
}
echo $match['ship_num']; // this echo prints '1-95080'
echo $match['company']; // this echo prints 'USPS'
print_r($match['tracking_code']); // this print_r prints an array with the value '1Z2216FE0348543895'
?>
And the second one:
<?php
$string = 'Shipped 1-95080 via other FedEx with tracking 729870539581, 729870539592.';
preg_match('/.+(?P<ship_num>\d{1}\-\d+).+via other (?P<company>\w+) with tracking (?<tracking_code>[\w,\s]+)/', $string, $match);
if(strpos($match['tracking_code'], ',')!==false) {
$match['tracking_code'] = array_map(function($index) {
return trim($index);
}, explode(',', $match['tracking_code']));
}
echo $match['ship_num']; // this echo prints '1-95080'
echo $match['company']; // this echo prints 'FedEx'
print_r($match['tracking_code']); // this print_r prints an array with the values '729870539581' and '729870539592'
?>
This RegExp will catch 3 groups:
(?P<ship_num>\d{1}\-\d+)
This group will catch one number(\d
), one hyphen(\-
) and some numbers(\d+
).
(?P<company>\w+)
This group will catch just some alphabetical characters(\w+
).
(?<tracking_code>[\w,\s]+)
Finally, this group will catch some spaces characters(\s
), commas and alphabetical characters(\w
).
In those all groups, I named each of them(?P<group name>
).
The tool Regex101 could be useful to test RegExp.