时间:2021-07-01 10:21:17 帮助过:3人阅读
- <br><br>//这部分代码是OK的,用于读取并解析本地的txt格式词典<br><!--?php
- <br /-->class Word<br>{<br>
- private $query_en='#\w+\b#i';<br>
- private $query_ch='#[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5},\)\.\( \w]*#u';<br>
- private $arr_word=array();<br>
- private $recycle_num=100;<br>
- private $fp=null;<br>
- <br>
- public function __construct($fileName)<br>
- {<br>
- $this->fp=fopen($fileName,'r') or die('打开ciba失败');<br>
- }<br>
- <br>
- <br>
- public function readWord()<br>
- {<br>
- while(!feof($this->fp))<br>
- {<br>
- $word=fgets($this->fp);<br>
- $word=trim($word);<br>
- if($word=='') continue;<br>
- <br>
- $en=$this->parseEn($word);<br>
- $ch=$this->parseCh($word);<br>
- $this->arr_word["$en"]=$ch;<br>
- <br>
- /* $this->recycle_num--;<br>
- if($this->recycle_num==0) return; */<br>
- <br>
- <br>
- }<br>
- }<br>
- public function parseEn(&$word)<br>
- {<br>
- if(preg_match($this->query_en, $word, $en))<br>
- {<br>
- return $en[0];<br>
- }<br>
- else<br>
- {<br>
- echo "match english word failed<br>";<br>
- }<br>
- }<br>
- public function parseCh(&$word)<br>
- {<br>
- if(preg_match($this->query_ch, $word, $ch))<br>
- {<br>
- return $ch[0];<br>
- }<br>
- else<br>
- {<br>
- echo "match chinese failed<br>";<br>
- }<br>
- }<br>
- <br>
- public function getWord()<br>
- {<br>
- return $this->arr_word;<br>
- }<br>
- <br>
- public function __destruct()<br>
- {<br>
- fclose($this->fp);<br>
- }<br>}<br><br>//$word=new Word('ciba.txt');<br>//$word->readWord();<br>//echo "<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>";<br>//print_r($word->getWord());<br>//echo "</li></ol></pre>"; */<br><br>?><br><br>//这部分代码也是OK的,用于将词条写入memcached<br><br><br><meta http-equiv="content-type" content="text/html; charset=utf-8"><br><br><br><!--?php <br /--><br>require_once('parseWord.php');<br>class MemStore<br>{<br>
- private $mem=null;<br>
- private $pat='#^[a-zA-Z]+\b#i';<br>
- <br>
- public function __construct()<br>
- {<br>
- $this->mem=new Memcache();<br>
- $this->mem->connect("127.0.0.1", 11211) or die("connect memcached failed!!!<br>");<br>
- }<br>
- public function __destruct()<br>
- {<br>
- $this->mem->close();<br>
- }<br>
- public function addWord()<br>
- {<br>
- $word=new Word('ciba.txt');<br>
- $word->readWord();<br>
- $result=$word->getWord();<br>
- //echo count($result)."字符<br>";<br>
- //exit();<br>
- foreach($result as $en => $ch)<br>
- {<br>
- $this->mem->add($en, $ch, MEMCACHE_COMPRESSED, time()+10*24*3600) or die("添加词条失败". __LINE__ ."<br>");<br>
- }<br>
- }<br>
- <br>
- public function setWord($en,$ch)<br>
- {<br>
- //控制器判断输入是否合法<br>
- $en=$this->filterWord($en);<br>
- $en=$this->mem->get($en) or die("找不到词条 $en");<br>
- $this->mem->set($en, $ch, MEMCACHE_COMPRESSED, time()+31*24*3600) or die("添加词条$en失败");<br>
- <br>
- }<br>
- <br>
- public function getWord($en)<br>
- {<br>
- //控制器判断输入是否合法<br>
- $en=$this->filterWord($en);<br>
- $ch=$this->mem->get($en) or die("找不到词条 $en");<br>
- return $ch;<br>
- }<br>
- <br>
- public function replaceWord($en,$ch)<br>
- {<br>
- //控制器判断输入是否合法<br>
- $en=$this->filterWord($en);<br>
- $en=$this->mem->get($en) or die("找不到词条 $en");<br>
- $this->mem->replace($en, $ch, MEMCACHE_COMPRESSED, time()+31*24*3600) or die("替换词条$en失败");
- <br>
- }<br>
- <br>
- public function deleteWord($en)<br>
- {<br>
- //控制器判断输入是否合法<br>
- $en=$this->filterWord($en);<br>
- $this->mem->delete($en,0) or die("删除词条$en失败");<br>
- }<br>
- <br>
- //过滤掉中文,包括空格的词组,长度大于20的词条<br>
- public function filterWord($en)<br>{<br>
- $en=trim($en);<br><br><br>
- if(preg_match('#[\x{4e00}-\x{9fa5},\)\.\(]+#u', $en))<br>
- {<br>
- //echo '暂时不支持中文查询<br>';<br>
- if(preg_match('#\b[a-z]+\b#i', $en, $res))<br>
- {<br>
- if(strlen($res[0])>20)<br>
- {<br>
- //echo "字符过长<br>";<br>
- return strtolower(substr($res[0], 0,20));<br>
- }<br>
- return strtolower($res[0]);<br>
- }<br>
- }<br>
- <br>
- else if(preg_match('#\s+#', $en))<br>
- {<br>
- //$en=explode(' ', $en);<br>
- //echo "含有空格<br>";<br>
- $res=null;<br>
- if(preg_match('#[a-z]+#i', $en, $res))<br>
- {<br>
- if(strlen($res[0])>20)<br>
- {<br>
- //echo "字符过长<br>";<br>
- return strtolower(substr($res[0], 0,20));<br>
- }<br>
- return strtolower($res[0]);<br>
- }<br>
- }<br>
- else if(preg_match('#[—_\+\?\*\^\$\#\%\&\/\\,\.!@=\`\'\"\"""]#',$en, $res))<br>
- {<br>
- //<br>
- //echo '含有非法字符<br>';<br>
- if(preg_match('#[a-z]+#i', $en, $res))<br>
- {<br>
- if(strlen($res[0])>20)<br>
- {<br>
- echo "字符过长<br>";<br>
- return strtolower(substr($res[0], 0,20));<br>
- }<br>
- return strtolower($res[0]);<br>
- }<br>
- }<br>
- <br>
- else if(strlen($en)>20)<br>
- {<br>
- //echo "字符过长<br>";<br>
- return strtolower(substr($en, 0,20));<br>
- }<br>
- else <br>
- {<br>
- return $en;<br>
- }<br>
- <br>}<br>
- <br>
- public function flushAll()<br>
- {<br>
- $this->mem->flush();<br>
- }<br>
- <br>
- public function getTime()<br>
- {<br>
- if (function_exists("micro_time"))<br>
- {<br>
- list($usec, $sec) = explode(" ", microtime());<br>
- return ((float)$usec + (float)$sec);<br>
- }<br>
- else<br>
- {<br>
- return time();<br>
- }<br>
- }<br>}<br><br>//$mem=new MemStore();<br>//$mem->addWord();<br>//$mem->flushAll();<br>//$mem->replaceWord('abandon', 100000000);<br>//$mem->deleteWord('abandon');<br>//echo $mem->getWord('_*&^%abandon^%$#');<br>//echo "ok"; <br><br>?><br><br><br><br><br>//下面这段代码也是OK的,根据客户端提交的英语单词,可以成功查询到对应的中文,并写入本地文件成功过<br><!--?php <br /-->header("content-type: text/xml; charset=utf-8");<br>header("Cache-Control: no-cache");<br>require_once "storeWord.php";<br>if(!empty($_GET['enword']))<br>{<br>
- $en=$_GET['enword'];<br>
- //file_put_contents('aword.txt', $en."\t\t",FILE_APPEND);<br>
- <br>
- $mem=new MemStore();<br>
- $ch=$mem->getWord($en);<br>
- $en=$mem->filterWord($en);<br>
- <br>
- <br>
- $res="<res><en>".$en."</en><ch>".$ch."</ch></res>";<br>
- file_put_contents('aword.txt', $res."\r\n",FILE_APPEND);//这里是OK的<br>
- echo $res;<br>
- //echo '{'.$en.':'.$res.'}';<br>}<br>else<br>{<br>
- file_put_contents('aword.txt', "receive NON data \r\n",FILE_APPEND);<br>}<br><br>?><br><br>//我估计问题出在下面这段代码,,但是就是找不出问题所在,一直显示undefined<br><br><br><br><meta http-equiv="content-type" content="text/html; charset=utf-8"><br><br><br><br><br><br><center><br><input type="text" name="enWord" id="enWord" size="100" maxlength="40"><input type="button" value="查询" onclick="query();"><br><br><br></center><br><br><br><br><br>