rakibdevs/mrz-parser
Composer 安装命令:
composer require rakibdevs/mrz-parser
包简介
A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Documents.
关键字:
README 文档
README
A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2).
It reads the machine-readable zone, extracts the document fields, and — new in v2 — verifies the ICAO 9303 check digits so you can tell whether the data is internally consistent.
Requirements
- PHP 8.2 or higher
Installation
You can install the package via composer:
composer require rakibdevs/mrz-parser
Usage
use Rakibdevs\MrzParser\MrzParser; ..... ..... $data = MrzParser::parse('P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<< L898902C36UTO7408122F1204159ZE184226B<<<<<10'); var_dump($data);
Output
{
"type": "Passport",
"card_no": "L898902C3",
"issuer": "Utopian",
"issuer_code": "UTO",
"date_of_expiry": "2012-04-15",
"first_name": "ANNA MARIA",
"last_name": "ERIKSSON",
"date_of_birth": "1974-08-12",
"gender": "Female",
"sex": "F",
"personal_number": "ZE184226B",
"optional_data": "ZE184226B",
"nationality": "Utopian",
"nationality_code": "UTO",
"valid": true,
"validation": {
"card_no": true,
"date_of_birth": true,
"date_of_expiry": true,
"personal_number": true,
"composite": true
},
"raw": "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<\nL898902C36UTO7408122F1204159ZE184226B<<<<<10"
}
Check-digit validation
Every result carries a valid flag (overall) and a validation map with the outcome of each ICAO 9303 check digit (card_no, date_of_birth, date_of_expiry, personal_number for passports, and the composite). Visas carry no composite check digit, so their validation map omits it. Use these to reject OCR mistakes or tampered documents.
Input handling
Input is normalized before parsing: \r\n / \r line endings are converted to \n, surrounding whitespace is trimmed, and blank lines are dropped — so MRZ strings copied from Windows or OCR output parse the same as clean input. Dates are validated strictly (impossible dates such as Feb 30 return null), and a date of birth that would resolve to the future is shifted back a century.
Advanced usage
Typed result object
read() returns an MrzResult value object with typed properties and helpers, while toArray() gives you the same array as parse():
$mrz = MrzParser::read($text); $mrz->cardNo; // 'L898902C3' $mrz->fullName(); // 'ANNA MARIA ERIKSSON' $mrz->dateOfBirthAsDate(); // DateTimeImmutable $mrz->isExpired(); // bool (or null when the format has no expiry) $mrz->valid; // bool $mrz->toArray(); // the canonical parse() array
Non-throwing parsing
parse()/read() throw on unrecognised input. Use the try* variants to get null instead:
MrzParser::tryParse($text); // ?array MrzParser::tryRead($text); // ?MrzResult
Strict check-digit mode
Pass strict: true to reject documents whose check digits don't verify (throws InvalidChecksumException; the try* variants return null):
MrzParser::parse($text, strict: true); MrzParser::read($text, strict: true);
Name transliteration
The MRZ name field only holds A–Z. Convert a visual-zone (accented) name to its MRZ form, or compare the two:
use Rakibdevs\MrzParser\Support\Transliterator; Transliterator::fromLatin('Müller'); // 'MUELLER' Transliterator::fromLatin('Jørgensen'); // 'JOERGENSEN' Transliterator::matches('MUELLER', 'Müller'); // true
Laravel
The package ships an auto-discovered service provider and a validation rule (requires illuminate/support):
use Rakibdevs\MrzParser\Laravel\Rules\ValidMrz; $request->validate([ 'mrz' => [new ValidMrz()], // must be a parseable MRZ 'mrz' => [new ValidMrz(strict: true)], // ...with valid check digits ]);
Supported Document
Passport (TD3)
P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<
L898902C36UTO7408122F1204159ZE184226B<<<<<10
Visa
V<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<
L8988901C4XXX7408122F96121096ZE184226B<<<<<<
Or
V<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<
L8988901C4XXX7408122F9612109<<<<<<<<
Travel Document (TD1)
I<UTOD231458907<<<<<<<<<<<<<<<
7408122F1204159UTO<<<<<<<<<<<6
ERIKSSON<<ANNA<MARIA<<<<<<<<<<
Travel Document (TD2)
I<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<
D231458907UTO7408122F1204159<<<<<<<6
French National ID (legacy 2×36 Carte Nationale d'Identité)
This pre-2021 format predates ICAO TD1/TD2 and has its own layout (no expiry date is encoded).
IDFRADOUEL<<<<<<<<<<<<<<<<<<<<932013
0506932020438CHRISTIANE<<NI2906209F3
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
Special thanks to tetrahydra for organised country list, Al Amin for help extracting information.
License
The MIT License (MIT). Please see License File for more information.
rakibdevs/mrz-parser 适用场景与选型建议
rakibdevs/mrz-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.12k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2022 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rakibdevs」 「mrz-parser」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rakibdevs/mrz-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rakibdevs/mrz-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rakibdevs/mrz-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 11.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-26