The main function is file_get_contents. The main program is divided into two sections. Come with me and read it. (For the original code of the mortal blog, please note it ).
Copy codeThe 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 );
}
The above function mainly implements the escape Encoding Process of JavaScript using php, because the Hong En query interface needs to pass the past value as the idiom entry after escape encoding.
Copy codeThe 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, '<table class = "english"> ');
$ Text = substr ($ text, $ pos1, $ pos2-$ pos1 );
// Convert the character set from the original UTF-8 to GB2312, note that after GB2312 added // IGNORE, forced to continue conversion in case of special characters, because the iconv function terminates the conversion when the Chinese character "1" is encountered.
$ Text = iconv ('utf-8', 'gb2312 // IGNORE ', $ text );
If (strpos ($ text, 'source ')){
Return $ text;
}
}
The above is a self-defined idiom query function, first escape encoding to query the idiom entries, and then use the file_get_contents function to get "http://study.hongen.com/dict/ndsearchchengyu.aspx? Type = exact & word = "indicates the content queried on the page. Use substr to remove unnecessary code, in the middle is the explanation part of the idiom (including pinyin, explanation, source, example), and finally remember to transcode, The result returned by Hong En is UTF-8 encoding, we need to convert to GB2312 encoding, I wrote a text note about iconv Function Conversion encoding that sometimes has bugs. I need to add a // IGNORE parameter. Finally, check whether the word "Source" exists in the result. If yes, the entire function runs successfully. You can return the obtained content to the page.
After the implementation of the Program subject is completed, you only need to call the query function: chacy at the corresponding position.