Copy codeThe Code is as follows:
<? Php
$ Arr = array ();
$ Arr ['hao'] = 'bad ';
$ Arr ['bad'] = 'bad ';
$ Arr ['bad'] = 'good ';
$ Arr ['bad'] = 'bad ';
$ Str = "Are we good ";
Echo strtr ($ str, $ arr );
// Output is not bad.
?>
So the problem is solved. Build a database to collect Synonyms
The two key programs are importing databases and exporting files.
Import word2db. php from file to database
Copy codeThe Code is as follows:
<? Php
// Write the array in the file into the database
Require ("conn. php ");
@ Require ("keyword. php ");
Mysql_query ("delete from". table ('keyword ')."");
Foreach ($ keyword as $ key => $ val)
{
// $ Key = iconv ('utf-8', 'gbk', $ key );
// $ Val = iconv ('utf-8', 'gbk', $ val );
$ Pinyin = getfirstchar ($ key );
$ Ct = mysql_query ("select count (*) from ". table ('keyword '). "where k1 = '$ key' and k2 =' $ val '"); // check whether the specified object already exists.
$ Ct = @ mysql_fetch_array ($ ct );
$ Ct = $ ct [0];
If ($ ct <= 0) // insert if it does not exist
{
Mysql_query ("insert ". table ('keyword '). "(k1, k2, pinyin) values ('$ key',' $ val ',' $ pinyin')") or die ("error ");
}
}
Echo "inserted successfully! ";
?>
Import db2word. php from database to file
Copy codeThe Code is as follows:
<? Php
// Write the database to a file as an array
Require ("conn. php ");
$ Res = mysql_query ("select k1, k2 from". table ('keyword ')."");
$ Str = "<? Php \ r \ n ";
While ($ rs = mysql_fetch_array ($ res ))
{
$ Str. = "\ $ keyword ['". $ rs [0]. "'] = '". $ rs [1]. "'; \ r \ n ";
}
$ Str. = "?> ";
File_put_contents ("keyword. php", $ str );
Echo "exported ";
?>