Hong En online idiom dictionary thief program php version

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.