Horn Online idiom dictionary thief program PHP version _php tips

Source: Internet
Author: User
The main function is file_get_contents, the main program is divided into two paragraphs, with me look over it (human blog original code, reproduced please specify).
Copy Code code 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 is primarily PHP to implement the JavaScript escape encoding process, because the Horn query interface needs to pass past the value is an escape code after the idiom entries.
Copy Code code as follows:

function Chacy ($chengyu) {
$chengyu =escape ($chengyu);
$text = @file_get_contents (' http://study.hongen.com/dict/ndsearchchengyu.aspx?type=exact&word= '. $chengyu);
$pos 1=strpos ($text, ' <table class= ' root ' > ');
$pos 2=strrpos ($text, ' <table class= ' 中文版 ' > ');
$text =substr ($text, $pos 1, $pos 2-$pos 1);
Convert the character set from the original UTF-8 to the GB2312, note that after the GB2312 plus//ignore, forced to encounter special characters also continue to convert, because in the face of the Chinese character "one" when the Iconv function will terminate the conversion
$text =iconv (' UTF-8 ', ' Gb2312//ignore ', $text);
if (Strpos ($text, ' provenance ')) {
return $text;
}
}

Above is its own definition of 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= "page query content, using SUBSTR to remove some unnecessary unnecessary code, the middle is the interpretation of the idiom clause (including pinyin, explanation, source, examples), and finally remember to turn the code, horn return the result is UTF-8 coding, In general, we need to convert to GB2312 encoding, and above I have written a comment text about the ICONV function conversion encoding that sometimes bugs, and you need to add a//ignore parameter. Finally judge whether there is a "source" in the result of the word, some words that the entire function is successful, you can get the content return to the page.

The program body implementation completes, only then calls the query function in the corresponding position: Chacy is OK.

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.