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.