Hongen online idiom dictionary thieves program php edition

  • 2020-05-16 06:32:57
  • OfStack

The main function is file_get_contents, the main program is divided into two paragraphs, follow me 1 to come over.
 
function escape($str){ 
preg_match_all('/[\x80-\xff].|[\x01-\x7f]+/',$str,$r); 
$ar = $r[0]; 
foreach($ar as $k=>$v){ 
if(ord($v[0]) < 128) 
$ar[$k] = rawurlencode($v); 
else 
$ar[$k] = '%u'.bin2hex(iconv('GB2312','UCS-2',$v)); 
} 
return join('',$ar); 
} 

The above function mainly USES php to implement JavaScript's escape encoding process, because hoon's query interface needs to pass the past values as escape encoded idiom entries.
 
function chacy($chengyu){ 
$chengyu=escape($chengyu); 
$text=@file_get_contents('http://study.hongen.com/dict/ndsearchchengyu.aspx?type=exact&word='.$chengyu); 
$pos1=strpos($text,'<table class="root">'); 
$pos2=strrpos($text,'<table class="english">'); 
$text=substr($text,$pos1,$pos2-$pos1); 
// Change the character set from the original UTF-8 The switch to GB2312 , pay attention to in GB2312 And then I added //IGNORE , forcing you to continue the conversion even when you encounter a special character. 1 " iconv The function terminates the transformation  
$text=iconv('UTF-8','GB2312//IGNORE',$text); 
if (strpos($text,' provenance ')){ 
return $text; 
} 
} 

Query function is above their own definition of idioms, escape coding to query idioms items first, and then use file_get_contents function to obtain "http: / / study. hongen. com/dict/ndsearchchengyu aspx? type = exact & word query = "page content, use substr to remove 1 before and after some redundant code, you don't need the middle part of the explanation is the idiom entry (including pinyin, interpretation, the source, for example), the last remember to transcoding, grace the returned result is UTF - 8 encoding, 1 case, we need to convert GB2312 coding, I wrote above 1 iconv function transform coding can appear sometimes bug annotation text, need to add 1 / / IGNORE parameters. Finally, judge whether there is the word "source" in the result. If there is, it means that the whole function runs successfully, and the obtained content return can be given to the page.

The main body of the program is implemented by simply calling the query function: chacy in the appropriate location.

Related articles: