Use PHP to parse administrative division code

Source: Internet
Author: User
I have already written this PHP script many times, but I have rewritten it because of some changes in the data format provided by the National Bureau of Statistics. The problem involves both PHP and, at the same time, MySQL is also involved, and it is very suitable for interview questions. Such questions often reflect the basic quality of job seekers. Preparations: download the latest administrative divisions of counties and above

I have already written this PHP script many times, but I have rewritten it because of some changes in the data format provided by the National Bureau of Statistics. The problem involves both PHP and, at the same time, MySQL is also involved, and it is very suitable for interview questions. Such questions often reflect the basic quality of job seekers. Preparations: download the latest administrative divisions of counties and above

I have already written this PHP script many times, but I have rewritten it because of some changes in the data format provided by the National Bureau of Statistics. The problem involves both PHP and, at the same time, MySQL is also involved, and it is very suitable for interview questions. Such questions often reflect the basic quality of job seekers.

Preparations: Download and save the latest administrative code in counties and counties, and save the code as a data.txt file. Please note that if the format is incorrect during the copy and paste process, try to paste the copied content to Word first, WPS and other software, and then copy and paste it into a text file.

The latest administrative code for counties and above

Note: For data of different versions, full-width spaces and half-width spaces may be mixed.

Create a MySQL table first:

Note that the character set of the table must be consistent with that of the file.

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:

Verify that the content of the data.txt file is valid, and the Code itself is not strictly verified.

  PDO::ERRMODE_EXCEPTION,);$dbh = new PDO($dsn, $username, $password, $options);$sth = $dbh->prepare('    INSERT INTO region (id, parent_id, name)    VALUES (:id, :parent_id, :name)');$parent = array(0);$handle = fopen('data.txt', 'r');while (!feof($handle)) {    $row = trim(str_replace(' ', ' ', fgets($handle)));    if (!preg_match('/^(\d+)\s+(.+)$/', $row, $matches)) {        continue;    }    list($row, $id, $name) = $matches;    $level = strlen(preg_replace('/(00){1,2}$/', '', $id)) / 2;    $parent_id = $parent[$level - 1];    $parent[$level] = $id;    $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: The level variable can be introduced to eliminate the annoying "if/else" statement in the program.

......

With the official administrative division code, coupled with a private pure IP database, it is more perfect.

Original article address: Use PHP to parse the administrative division code. Thank you for sharing it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.