Online Idiom dictionary Hongen 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 paragraphs, with me to see it (the Human blog Original code, reproduced please specify).

Copy the 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 mainly uses PHP to implement the JavaScript escape encoding process, because the Hongen query interface needs to pass the past value is the escape code after the idiom entry.

Copy the 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, '


'); $pos 2=strrpos ($text, '










The above introduces the online idiom dictionary hongen online idiom Dictionary thief program PHP version, including the online idiom dictionary content, I hope the PHP tutorial interested in a friend helpful.

  • '); $text =substr ($text, $pos 1, $pos 2-$pos 1); Convert the character set from the original UTF-8 to GB2312, notice that after GB2312 with the//ignore, forcing to encounter special characters also continue to convert, because when encountering 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 the self-defined idiom query function, first escape code to query the idiom entry, and then use the file_get_contents function to get "http://study.hongen.com/dict/ndsearchchengyu.aspx?" type=exact&word= "page query content, the use of substr to remove some unnecessary unnecessary code, the middle is the Idiom entry interpretation part (including Pinyin, interpretation, provenance, example), finally remember to Transcode, Hongen return the result is UTF-8 code, In general we need to convert to GB2312 encoding, above I wrote a comment about the Iconv function conversion code will sometimes appear bug text, need to add a//ignore parameter. Finally, whether there is a "source" in the results of the word, some words that the entire function is successful, you can get the content to return to the page. The main implementation of the program, simply call the query function in the appropriate location: Chacy on it.

    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.