When debugging an ajax task, I wrote an English-Chinese Dictionary. the specific process is as follows:
1. Write Local files to the memcached server in the format of {English: Chinese}
2. submit English words through ajax and return Chinese meaning
Problem: to query the corresponding word, you can use the file_put_contents function to write it locally to prove that the corresponding word is queried. on the client side, the readyState attribute shows that 1, 2, 3, 4 are returned in sequence, but in window. alert (type res) is displayed as undefined.
// This part of the code is OK, used to read and parse the local txt format Dictionary
Fp = fopen ($ fileName, 'r') or die ('failed to open CBA ');} public function readWord () {while (! Feof ($ this-> fp) {$ word = fgets ($ this-> fp); $ word = trim ($ word); if ($ word = '') continue; $ en = $ this-> parseEn ($ word); $ ch = $ this-> parseCh ($ word ); $ this-> arr_word ["$ en"] = $ ch;/* $ this-> recycle_num --; if ($ this-> recycle_num = 0) return; */} public function parseEn (& $ word) {if (preg_match ($ this-> query_en, $ word, $ en) {return $ en [0];} else {echo "match english word failed
";}} Public function parseCh (& $ word) {if (preg_match ($ this-> query_ch, $ word, $ ch) {return $ ch [0];} else {echo "match chinese failed
";}} Public function getWord () {return $ this-> arr_word;} public function _ destruct () {fclose ($ this-> fp );}} // $ word = new Word('ciba.txt '); // $ word-> readWord (); // echo"";//print_r($word->getWord());//echo "
"; */?> // This part of the code is also OK, used to write the entry into memcached
Mem = new Memcache (); $ this-> mem-> connect ("127.0.0.1", 11211) or die ("connect memcached failed !!!
");} Public function _ destruct () {$ this-> mem-> close ();} public function addWord () {$ word = new Word('ciba.txt '); $ word-> readWord (); $ result = $ word-> getWord (); // echo count ($ result ). "character
"; // Exit (); foreach ($ result as $ en => $ ch) {$ this-> mem-> add ($ en, $ ch, MEMCACHE_COMPRESSED, time () + 10*24*3600) or die ("An error occurred while adding the entry ". _ LINE __."
") ;}} Public function setWord ($ en, $ ch) {// The controller determines whether the input is valid $ en = $ this-> filterWord ($ en ); $ en = $ this-> mem-> get ($ en) or die ("No entry found $ en"); $ this-> mem-> set ($ en, $ ch, MEMCACHE_COMPRESSED, time () + 31*24*3600) or die ("failed to add entry $ en");} public function getWord ($ en) {// The controller determines whether the input is valid. $ en = $ this-> filterWord ($ en); $ ch = $ this-> mem-> get ($ en) or die ("No entry found $ en"); return $ ch;} public function replaceWord ($ en, $ ch) {// The controller determines whether the input is valid. $ en = $ this-> filterWord ($ en); $ en = $ this-> mem-> get ($ en) or die ("No entry found $ en"); $ this-> mem-> replace ($ en, $ ch, MEMCACHE_COMPRESSED, time () + 31*24*3600) or die ("failed to replace entry $ en");} public function deleteWord ($ en) {// The controller determines whether the input is valid. $ en = $ this-> filterWord ($ en); $ this-> mem-> delete ($ en, 0) or die ("failed to delete entry $ en");} // filter out Chinese characters, including space phrases, and public function filterWord ($ en) for a term with a length greater than 20) {$ en = trim ($ en); if (preg_match ('# [\ x {4e00}-\ x {9fa5 },\)\. \ (] + # U', $ en) {// echo 'Chinese query is not supported currently
'; If (preg_match (' # \ B [a-z] + \ B # I ', $ en, $ res )) {if (strlen ($ res [0])> 20) {// echo "is too long
"; Return strtolower (substr ($ res [0],);} return strtolower ($ res [0]);} else if (preg_match ('# \ s + #', $ en) {// $ en = explode ('', $ en); // echo" contains spaces
"; $ Res = null; if (preg_match ('# [a-z] + # I', $ en, $ res )) {if (strlen ($ res [0])> 20) {// echo "is too long
"; Return strtolower (substr ($ res [0],);} return strtolower ($ res [0]);} else if (preg_match ('#[? _ \ + \? \ * \ ^ \ $ \ # \ % \&\/\\,\.! @ = \ '\ "\"] #', $ En, $ res) {// echo 'contains invalid characters
'; If (preg_match (' # [a-z] + # I ', $ en, $ res) {if (strlen ($ res [0])> 20) {echo "is too long
"; Return strtolower (substr ($ res [0],);} return strtolower ($ res [0]);} else if (strlen ($ en)> 20) {// echo "is too long
"; Return strtolower (substr ($ en,);} else {return $ en;} public function flushAll () {$ this-> mem-> flush ();} public function getTime () {if (function_exists ("micro_time") {list ($ usec, $ sec) = explode ("", microtime (); return (float) $ usec + (float) $ sec) ;}else {return time ();}}} // $ mem = new MemStore (); // $ mem-> addWord (); // $ mem-> flushAll (); // $ mem-> replaceWord ('abandon', 100000000); // $ mem-> deleteWord ('abandon '); // Echo $ mem-> getWord (' _ * & ^ % abandon ^ % $ # '); // echo "OK";?>// The following code is also OK. based on the English words submitted by the client, you can query the corresponding Chinese characters and write the local files successfully.
GetWord ($ en); $ en = $ mem-> filterWord ($ en); $ res ="
". $ En ."
". $ Ch ."
"Inclufile_put_contents('aword.txt ', $ res. "\ r \ n", FILE_APPEND); // Here is the OK echo $ res; // echo '{'. $ en. ': '.{res.'{'}%else=file_put_contents('aword.txt', "receive NON data \ r \ n", FILE_APPEND) ;}?> // I guess the problem lies in the following code, but the undefined is always displayed.