Home > Backend Development > PHP Tutorial > Hong En Online Idiom Dictionary Thief Program PHP Version_PHP Tutorial

Hong En Online Idiom Dictionary Thief Program PHP Version_PHP Tutorial

WBOY
Release: 2016-07-21 15:19:03
Original
931 people have browsed it

The main function is file_get_contents. The main program is divided into two sections. Let’s take a look at it with me (original code from Mortal Blog, please indicate when reprinting).

Copy code The code is as follows:

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);
}

Above The function mainly uses PHP to implement the escape encoding process of JavaScript, because Hongen's query interface needs to pass the past value which is the idiom entry after escape encoding.
Copy code The code is as follows:

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,'');
$text=substr($text,$pos1 ,$pos2-$pos1);
//Convert the character set from the original UTF-8 to GB2312. Note that //IGNORE is added after GB2312 to force the conversion to continue when encountering special characters. When the Chinese character "一" appears, the iconv function will terminate the conversion
$text=iconv('UTF-8','GB2312//IGNORE',$text);
if (strpos($text,'source') ){
return $text;
}
}

The above is a self-defined idiom query function. First, escape encodes the idiom entry to be queried, and then uses the file_get_contents function to obtain " http://study.hongen.com/dict/ndsearchchengyu.aspx?type=exact&word=”The content of the page query, use substr to remove some unnecessary redundant codes before and after, the middle is the explanation part of the idiom entry (including pinyin, explanation, Source, example sentences), finally remember to transcode. The result returned by Hongen is UTF-8 encoding. Generally, we need to convert it to GB2312 encoding. Above I wrote a comment text about bugs that sometimes occur when iconv function converts encoding. It is necessary Add an //IGNORE parameter. Finally, determine whether the word "source" exists in the result. If so, it means that the entire function runs successfully and the obtained content can be returned to the page.

The main body of the program is completed, just call the query function: chacy at the corresponding location.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325337.htmlTechArticleThe main function is file_get_contents. The main program is divided into two sections. Take a look at it with me (Mortal Blog original code, reprinted Please specify). Copy the code The code is as follows: function escape($str){...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template