Last year, I learned how to use php as a thief program. I hope it can help my friends who have the same requirements, the main process of the program is to obtain the idiom dictionary query result of Hong en online and display it on the current page (commonly known as the thief program). The main function of using the language php is file_get_contents, and the main program is divided into two sections, I have read it with you (for original code of a mortal blog, please note it ).
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 );
}
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.
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 ,'
');$ Pos2 = strrpos ($ text ,'
');$ 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.