PHP code example of hejunjie / address-parser

1. Go to this page and download the library: Download hejunjie/address-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hejunjie / address-parser example snippets


use Hejunjie\AddressParser\AddressParser;

$raw = '张三,13512345678,410123199001011234 重庆攀枝花市东区机场路88号 邮编100000';

$parsed = AddressParser::parse($raw);

print_r($parsed);

[
    'name'     => '张三',
    'mobile'   => '13512345678',
    'idn'      => '410123199001011234',
    'postcode' => '100000',
    'province' => '四川省',
    'city'     => '攀枝花市',
    'region'   => '东区',
    'street'   => '机场路88号',
]

public static function parse(
    string $string,         // Raw address string to parse
    bool $user = true,      // Whether to extract user info (name, phone, ID, postcode)
    string $unknownValue = '未知',  // Fallback value when a region cannot be matched
    array $level1Data = [], // Custom province-level data; uses built-in data if empty
    array $level2Data = [], // Custom city-level data
    array $level3Data = []  // Custom district/county-level data
): array

$addresses = [
    '张三 13512345678 北京市朝阳区望京SOHO 邮编100000',
    '李四 13800138000 上海市浦东新区陆家嘴金融中心88号',
];

foreach ($addresses as $raw) {
    $result = AddressParser::parse($raw);
    // Process each result...
}