Home  >  Article  >  Backend Development  >  Use PHP to parse administrative division codes_PHP tutorial

Use PHP to parse administrative division codes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:53:101255browse

I have written this PHP script many times, but because the data format provided by the National Bureau of Statistics has changed slightly, I rewrote it again. The problem involves both PHP and MySQL. It feels like It is very suitable for interview questions. Such questions can often best reflect the basic qualities of job applicants.

Preparation work: You need to download the latest administrative division code for counties and above and save it as a data.txt file.

Use PHP to parse administrative division codes_PHP tutorial
The latest administrative division codes for counties and above

First create the MySQL table:
It should be noted that the character set of the table and the character set of the file need to be consistent.

CREATE TABLE IF NOT EXISTS `region` (
`id` int(10) unsigned NOT NULL,
​ `parent_id` int(10) unsigned NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB;
Supplement: Better storage of hierarchical data: Storing Hierarchical Data in a Database Article.

Then write the PHP script:
It should be noted that you must ensure the legality of the content of the data.txt file, and the code itself has not been subject to strictly prohibited verification.

// config
$host = '';
$dbname = '';
$charset = '';
$username = '';
$password = '';

set_time_limit(0);

$dsn = "mysql:host={$host};dbname={$dbname};charset={$charset}";

$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);

$dbh = new PDO($dsn, $username, $password, $options);

$handle = fopen('data.txt', 'r');

$parent = array();

while (!feof($handle)) {
$row = trim(fgets($handle));

if (!preg_match('/(d+)(s+)(.+)/', $row, $matches)) {
Continue;
}

list($row, $id, $delimiter, $name) = $matches;

if (!isset($separator)) {
          $separator = $delimiter;
}

$level = substr_count($delimiter, $separator);

$parent_id = $level > 1 ? $parent[$level - 1] : 0;

$parent[$level] = $id;

$sth = $dbh->prepare('
​​​​ INSERT INTO region (id, parent_id, name)
VALUES (:id, :parent_id, :name)
');

$sth->bindValue(':id', $id, PDO::PARAM_INT);
$sth->bindValue(':parent_id', $parent_id, PDO::PARAM_INT);
$sth->bindValue(':name', $name);

$sth->execute();
}

fclose($handle);

?>
Note: Fill in the configuration options according to your own situation and then run.

……

With the official administrative division code and the private IP database, it is even more perfect.

Author: Lao Wang

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478052.htmlTechArticleI have written this PHP script many times, but because the data format provided by the National Bureau of Statistics is somewhat changes, so I rewrote it again. The problem involves both PHP and...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn