Pinyin Library Create PHP

Source: Internet
Author: User
Tags db2

The primary implementation obtains the Chinese name from a database and obtains the uppercase combinations of all possible pinyin. Then import a different database with the name

<?php//matches each man's initials by finding the phonetic table Get_firstchar ($keyword) {$array = array (); $temp = ""; $array = Explode (' [', File_ Get_contents ("Phonetic library text. txt"));//echo $array [3];for ($i =0; $i <count ($array); $i + +) {if (Strstr ($array [$i], $keyword)! = NULL) $temp = $temp.  Strtoupper (substr ($array [$i],0,1);//The value of the element that prints the array//echo $array [$i], "<br>"; }return $temp;} Arranges the combination function, implements all possible capital letters combination, for example the Chinese bank capital spelling may be "zgyh" "Zgyx" error_reporting (e_all&~e_notice); function Get_new_arr () {$ args = Func_get_args ();//Gets the array of all incoming arguments $arr = Array (), foreach ($args as $k = = $v) {if ($args [$k +1]) {switch ($k) {case 0: $arr [$k] = Arr_pailie ($v, $args [$k +1]); Break;default: $arr [$k] = Arr_pailie ($arr [$k-1], $args [$k +1]); $key = count ($arr)-1; $res = Implode ('. ', $arr [$key]); return $res;} function Arr_pailie ($arr 1, $arr 2) {$arr = array (); $k = 0;foreach ($arr 1 as $k 1=> $v 1) {foreach ($arr 2 as $k 2=> $v 2) {$arr [$k] = $v 1. ",". $v 2; $k + +;}} return $arr;} Database connection, remove name and identity $dbname= "DB1", $servcon =mysql_connect (MySQL database address, username, account), if (! $servcon) {die ("Fail To connect to database! Error: ". Mysql_error ());} mysql_select_db ($dbname, $servcon); $select = mysql_query ("Select Symbol,name from Smallsnapshoot");//Database connection, The results of the stock conversion are entered directly into the Py_convert table of the database stockdb $dbname1= "DB2"; $servcon 1=mysql_connect (<span style= "font-family:arial, Helvetica, Sans-serif; " >mysql database address, username, account </span>), if (! $servcon 1) {die ("Fail to connect to database! Error: ". Mysql_error ());} mysql_select_db ($dbname 1, $servcon 1);//Loop through the source database for each row name, identity while ($row = Mysql_fetch_array ($select)) {//Enter Chinese characters individually to find and save to $ Arr_list array//$str = "Flat single bank"; $str = $row ["Name"]; $len = Mb_strlen ($str, "Utf-8"); $arr _list = Array (); for ($i =0; $i < $len; $ i++) {$arr [] = Mb_substr ($str, $i, 1, "Utf-8"), $arr _list[$i] = Get_firstchar ($arr [$i]);} echo $arr _list[3];//Loop takes each character array letter to match save to $ARR_STR//arranges the combination function call, first converts each element of the string array to a pair of arrays, because this environment name is up to three characters or four characters $arr1 = array (); $arr 2 = Array (), $arr 3 = Array (), $arr 1 = str_split ($arr _list[0]), $arr 2 = Str_split ($arr _list[1]), $arr 3 = Str_split ($arr _ LIST[2]); if (count ($arr _list) = = 4) {$arr 4 = array (); $arr 4 = Str_split ($arr _list[3]); $result = Get_new_arr ($arr 1, $arr 2, $arr 3, $arr 4);//echo count ($arr _list);} else{$result = Get_new_arr ($arr 1, $arr 2, $arr 3),//echo count ($arr _list);} echo $result; Convert the combined data to an array and remove the duplicate arrays $arr_str = Array (), $arr _str = explode ('. ', $result)//echo $arr _str[1];for ($i =0; $i <count ($arr _ STR) ($i + +) {$arr _str[$i] =str_replace (",", "", $arr _str[$i]);} $membercout = count ($arr _str), $arr _str = Array_unique ($arr _str);//print_r ($arr _str); for ($i =0; $i < $membercout; $i + +) {if ($arr _str[$i]! = null) {//echo $arr _str[$i], "<br/>";//$str _end. = $arr _str[$i]; $py _insert = "INSERT INTO Py_c Onvert (Py_short_name, Py_stock_name, Py_stock_symbol) VALUE (' $arr _str[$i] ', ' $row [' name '] ', ' $row [' symbol '] ') '; result = mysql_query ($py _insert) or Die ("Error in Query: $query.") Mysql_error ()); }}//echo $str _end;} Mysql_close ($servcon); Mysql_close ($servcon 1);? > Set up the Database DB2 in the following pinyin table: <pre name= "code" class= "PHP" ><?php$con=mysql_connect (database name, account number, password); if (! $con) {die (' Could not connect: '. Mysql_error ());  }//Create Database/*if (mysql_query ("CREATE Database my_db", $con)) {echo "database created"; }else {echo "Error Creating Database:".  Mysql_error ();  }*///CREATE table in my_db database$dbname= "DB2"; mysql_select_db ($dbname, $con); $sql = "CREATE Table Py_convert (' py_id ')  Int (ten) not NULL auto_increment, ' py_short_name ' varchar (ten), ' py_name ' varchar, ' py_symbol ' varchar (+), PRIMARY KEY (' py_id ') "; mysql_query ($sql, $con); Mysql_close ($con);? >

Because of the names of Chinese characters involved, the editing environment used is UTF-8. The arrangement of the combination function is to draw on others, I also think that half a day effect is not good, the key is the data logical thinking is not clear, the importance of visible data thinking.

Oneself also learn while doing, PHP function call is not very skilled, later to strengthen the practice of programming. There is not a good place to write, welcome to the great God, mutual exchange and common progress!

Pinyin Library Create PHP

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.