I have a barcode that after being scanned it needs to be split into five variables.
Example barcode scan: 0109556135082301172207211060221967 21Sk4YGvF8
Alternate scan: 010955704600017017250630102107015
There are 5 delimiters
01 - gtin (14 DIGIT only)
11 - production date (YYMMDD)
17 - expire date (YYMMDD)
10 - batch no (can be variable but not more than 20 digit)
<space>21 - serial no (can be variable but not more than 20 digit)
01
09556135082301 (gtin)
17
220721 (production date)
10
60221967 (batch number)
21
Sk4YGvF8 (serial number)
This is my attempt:
$str = "0109556135082301172207211060221967 21Sk4YGvF8"
if ($strtemp != null){
$ais = explode("_",$str);
for ($aa=0;$aa<sizeof($ais);$aa++)
{
$ary = $ais[$aa];
while(strlen($ary) > 0) {
if (substr($ary,0,2)=="01"){
$igtin = substr($ary,2,14);
$ary = substr($ary,-(strlen($ary)-16));
}
else if (substr($ary,0,2)=="17"){
$expirydate = substr($ary,6,2)."-".substr($ary,4,2)."-20".substr($ary,2,2);
$ary = substr($ary,-(strlen($ary)-8));
}
else if (substr($ary,0,2)=="10"){
$batchno = substr($ary,2,strlen($ary)-2);
$ary = "";
}
else if (substr($ary,0,2)=="21"){
$serialno = substr($ary,2,strlen($ary)-2);
$ary = "";
}
else if (substr($ary,0,2)=="11"){
$proddate = substr($ary,6,2)."-".substr($ary,4,2)."-20".substr($ary,2,2);
$ary = substr($ary,-(strlen($ary)-8));
}
else {
$oth = "";
}
}
The result desired:
| Items | Result |
|---|---|
| GTIN | 09556135082301 |
| EXPIRE DATE | 21-07-2022 |
| BATCH/LOT NUMBER | 60221967 |
| SERIAL NUMBER | Sk4YGvF8 |
From my code above the variable come out like this:
| Items | Result |
|---|---|
| GTIN | 09556135082301 |
| EXPIRE DATE | 21-07-2022 |
| BATCH/LOT NUMBER | 6022196721Sk4YGvF8 |
| SERIAL NUMBER |