PHP code example of quangtam / vietnam-address-converter
1. Go to this page and download the library: Download quangtam/vietnam-address-converter 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/ */
quangtam / vietnam-address-converter example snippets
ietnam\AddressConverter\VietnamAddressConverter;
// Khởi tạo converter
$converter = new VietnamAddressConverter();
// Tải dữ liệu từ file JSON
$converter->initialize(); // Sử dụng file data/address.json mặc định
// Hoặc sử dụng file dữ liệu tùy chỉnh
$converter->initialize('/path/to/custom/address.json');
// Chuyển đổi địa chỉ từ string
$result = $converter->convertAddress('Phường 12, Quận Gò Vấp, Thành phố Hồ Chí Minh');
if ($result->isSuccess()) {
$converted = $result->getConvertedAddress();
$mapping = $result->getMappingInfo();
echo "Địa chỉ cũ: " . $result->getOriginalAddress()->getFormattedAddress() . "\n";
echo "Địa chỉ mới: " . $converted->getFormattedAddress() . "\n";
echo "Loại chuyển đổi: " . $mapping->getMappingType() . "\n";
} else {
echo "Lỗi: " . $result->getMessage() . "\n";
}
use Vietnam\AddressConverter\Models\FullAddress;
// Địa chỉ cũ (có Quận/Huyện)
$addressObject = new FullAddress(
'Phường 12', // ward
'Quận Gò Vấp', // district
'Thành phố Hồ Chí Minh', // province
'123 Nguyễn Văn Cừ' // street
);
$result = $converter->convertAddress($addressObject);
if ($result->isSuccess()) {
$converted = $result->getConvertedAddress();
// Kết quả: Phường An Hội Tây, Thành phố Hồ Chí Minh (không có district)
}
$result = $converter->convertAddress('Phường 12, Quận Gò Vấp, TP.HCM');
// Xuất JSON
echo $result->toJson();
// Xuất array
$array = $result->toArray();
print_r($array);
// Phường 12 và Phường 14 → Phường An Hội Tây
$converter->convertAddress('Phường 12, Quận Gò Vấp, TP.HCM');
// mappingType: 'merged'
$converter->convertAddress('Phường cũ, Quận ABC, Tỉnh XYZ');
// mappingType: 'renamed'
$converter->convertAddress('Phường An Lạc, Quận Bình Tân, TP.HCM');
// mappingType: 'unchanged'
interface ConversionResult {
public function isSuccess(): bool;
public function getOriginalAddress(): FullAddress;
public function getConvertedAddress(): ?NewAddress;
public function getMappingInfo(): ?MappingInfo;
public function getMessage(): ?string;
public function toArray(): array;
public function toJson(): string;
}
interface FullAddress {
public function getWard(): string;
public function getDistrict(): string;
public function getProvince(): string;
public function getStreet(): string;
public function getFormattedAddress(): string;
public function toArray(): array;
}
interface NewAddress {
public function getWard(): string;
public function getProvince(): string;
public function getStreet(): string;
public function getFormattedAddress(): string;
public function toArray(): array;
}