时间:2021-07-01 10:21:17 帮助过:15人阅读
//这部分代码是OK的,用于读取并解析本地的txt格式词典fp=fopen($fileName,'r') or die('打开ciba失败'); } 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 ""; */?>//这部分代码也是OK的,用于将词条写入memcachedmem=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)."字符
"; //exit(); foreach($result as $en => $ch) { $this->mem->add($en, $ch, MEMCACHE_COMPRESSED, time()+10*24*3600) or die("添加词条失败". __LINE__ ."
"); } } public function setWord($en,$ch) { //控制器判断输入是否合法 $en=$this->filterWord($en); $en=$this->mem->get($en) or die("找不到词条 $en"); $this->mem->set($en, $ch, MEMCACHE_COMPRESSED, time()+31*24*3600) or die("添加词条$en失败"); } public function getWord($en) { //控制器判断输入是否合法 $en=$this->filterWord($en); $ch=$this->mem->get($en) or die("找不到词条 $en"); return $ch; } public function replaceWord($en,$ch) { //控制器判断输入是否合法 $en=$this->filterWord($en); $en=$this->mem->get($en) or die("找不到词条 $en"); $this->mem->replace($en, $ch, MEMCACHE_COMPRESSED, time()+31*24*3600) or die("替换词条$en失败"); } public function deleteWord($en) { //控制器判断输入是否合法 $en=$this->filterWord($en); $this->mem->delete($en,0) or die("删除词条$en失败"); } //过滤掉中文,包括空格的词组,长度大于20的词条 public function filterWord($en){ $en=trim($en); if(preg_match('#[\x{4e00}-\x{9fa5},\)\.\(]+#u', $en)) { //echo '暂时不支持中文查询
'; if(preg_match('#\b[a-z]+\b#i', $en, $res)) { if(strlen($res[0])>20) { //echo "字符过长
"; return strtolower(substr($res[0], 0,20)); } return strtolower($res[0]); } } else if(preg_match('#\s+#', $en)) { //$en=explode(' ', $en); //echo "含有空格
"; $res=null; if(preg_match('#[a-z]+#i', $en, $res)) { if(strlen($res[0])>20) { //echo "字符过长
"; return strtolower(substr($res[0], 0,20)); } return strtolower($res[0]); } } else if(preg_match('#[?_\+\?\*\^\$\#\%\&\/\\,\.!@=\`\'\"\"""]#',$en, $res)) { // //echo '含有非法字符
'; if(preg_match('#[a-z]+#i', $en, $res)) { if(strlen($res[0])>20) { echo "字符过长
"; return strtolower(substr($res[0], 0,20)); } return strtolower($res[0]); } } else if(strlen($en)>20) { //echo "字符过长
"; return strtolower(substr($en, 0,20)); } 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"; ?>//下面这段代码也是OK的,根据客户端提交的英语单词,可以成功查询到对应的中文,并写入本地文件成功过getWord($en); $en=$mem->filterWord($en); $res=""; file_put_contents('aword.txt', $res."\r\n",FILE_APPEND);//这里是OK的 echo $res; //echo '{'.$en.':'.$res.'}';}else{ file_put_contents('aword.txt', "receive NON data \r\n",FILE_APPEND);}?>//我估计问题出在下面这段代码,,但是就是找不出问题所在,一直显示undefined ".$en." ".$ch."
var res=xmlhttp.responseXML;
window.alert(typeof $res);
一样吗?不一样当然不行
var res=xmlhttp.responseXML;
window.alert(typeof $res);
一样吗?不一样当然不行
sublime or notepad++
var res=xmlhttp.responseXML;
window.alert(typeof $res);
一样吗?不一样当然不行
//客户端做出如下修改 xmlhttp.onreadystatechange=function() { //window.alert(xmlhttp.readyState); if (xmlhttp.readyState==4 && xmlhttp.status==200) { var res=xmlhttp.responseText; res=eval("("+res+")"); window.alert(res); //var en=res.getElementsByTagName("en")[0].childNodes[0].nodeValue; //var ch=res.getElementsByTagName("ch")[0].childNodes[0].nodeValue; //var en=$("enWord").value; /var ch=res.en; $("chWord").innerText= en+": 的中文意思是: "+ch; } } //服务器这边改成用json传回数据,修改如下getWord($en); $en=$mem->filterWord($en); $res=""; file_put_contents('aword.txt', $res."\r\n",FILE_APPEND); //ob_start(); $res='{"'.$en.'":"'.$ch.'"}'; echo $res;}else{ file_put_contents('aword.txt', "receive NON data \r\n",FILE_APPEND);} $en $ch
我就不明白了这段HTML是拿来的.* ,而且结果是在后面,ob过滤就失效了
36行应为 ob_clean();
36行应为 ob_clean();