/** * dict.class.php collection Baidu dictionary translation content * * @copyright (C) Widuu * @license http://www.widuu.com * @lastmodify 2014-2-15 */ Header ("Content-type:text/html;charset=utf8"); Class dict{ Private $word; Number of bars displayed private static $num = 10; Public Function __construct () {} /** * Public return to Baidu data collection method * @param string English words * Retun Array ( * symbol "= phonetic transcription * "Pro" = pronunciation * "Example" = Example * "Explain" = concise explanation * "synonym" = same as antonyms * "phrase" = array of phrases * ) * */ Public function content ($word) { $this word = $word; $symbol = $this-pronounced (); $pro = $this->getsay (); $example = $this-Getexample (); $explain = $this-Getexplain (); $synonym = $this-getsynonym (); $phrase = $this-Getphrase (); $result = Array ( "Symbol" = $symbol,//phonetic transcription "Pro" + = $pro,//pronunciation "Example" = $example,//Example "Explain" = $explain,//Concise interpretation "Synonym" = $synonym,//Same antonym "Phrase" = $phrase//array of phrases ); return $result; } /** * Remote access to Baidu translation content * Get function Curl * Retun String * */ Private Function getcontent () { $useragent = "mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) gecko/20100101 firefox/23.0 "; $ch = Curl_init (); $url = "http://dict.baidu.com/s?wd=". $this->word; curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, curlopt_useragent, $useragent); curl_setopt ($ch, Curlopt_returntransfer, TRUE); curl_setopt ($ch, curlopt_followlocation, 1); curl_setopt ($ch, Curlopt_httpget, 1); curl_setopt ($ch, curlopt_autoreferer,1); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_timeout, 30); $result = curl_exec ($ch); if (Curl_errno ($curl)) { Echo ' Errno '. Curl_error ($curl); } Curl_close ($ch); return $result; } /** * Get Baidu Translate pronunciation * Retun Array (English, American) * */ Private function pronounced () { $data = $this-GetContent (); Preg_match_all ("/\" En\-us\ "\> (. *) \<\/b\>/ui", $data, $pronounced); Return Array ( ' En ' = $pronounced [1][0], ' Us ' = $pronounced [1][1] ); } /** * Get Baidu Translate pronunciation * Return Array (UK, US) * */ Private Function Getsay () { $data = $this-GetContent (); Preg_match_all ("/url=\" (. *) \ "/ui", $data, $pronounced); Return Array ( ' En ' = $pronounced [1][0], ' Us ' = $pronounced [1][1] ); } /** * Get Baidu Translation examples * Return Array () multidimensional array example * */ Private Function Getexample () { $str = ""; $data = $this-GetContent (); Preg_match_all ("/var example_data = (. *) \]\;/us", $data, $example); $data 1 = "[[". LTrim ($example [1][0], "["); $data 2 = Explode ("[[[", $data 1); $num = Count (Array_filter ($data 2)); foreach ($data 2 as $key = = $value) { $data 3 = Explode ("[[", "[[". $value); foreach ($data 3 as $k = = $v) { Preg_match_all ("/\[\" (. *) \ ",/US", "[". $v, $match); if (!empty ($match [1])) { $str. = Implode ($match [1], ""). " @"; } } } $data 4 = Trim ($str, "@"); $data 5 = Explode ("@", $data 4); $result = Array_chunk ($data 5, 2); return $result; } /** * Get a concise explanation * return Array (x = "Part of speech", B = "accessory") * **/ Private Function Getexplain () { $data = $this-GetContent (); Preg_match_all ("/id\=\" en\-simple\-means\ "\> (. *) \/us", $data, $explain); $r _data = $explain [1][0]; Preg_match_all ("/\ \ (? p.*) \<\/strong\>\ (? P . *) \<\/span\>\<\/p\>/us ", $r _data, $a _data); Preg_match_all ("/\ (? P [^\>]+] \:\ (? P . *) \<\/a\>\<\/span\>/us ", $r _data, $b _data); $result = Array (); foreach ($a _data["adj"] as $key = + $value) { $result [$value] = $a _data["name" [$key]; } $word _b = Array (); foreach ($b _data["tag"] as $key = + $value) { $word _b[$value] = strip_tags ($b _data["word"] [$key]); } $result _data = Array ("x" = $result, "b" = = $word _b); return $result _data; } /** * Get synonyms * Return Array (0 = "synonyms", 1 = "antonyms") is generally a multidimensional array * */ Private Function getsynonym () { $data = $this-GetContent (); Preg_match_all ("/id=\" en\-syn\-ant\ "\> (. *)/US", $data, $synonym); $content = $synonym [1][0]; $data 1 = Explode ("", $content); $result = Array (); $data 2 = array (); foreach ($data 1 as $key = = $value) { Preg_match_all ("/\ (? p.*) \ \;\<\/strong\>\<\/div\>\\ (? . *) \<\/ul\>/us ", $value, $r _data); $data 2[$key] ["adj"] = $r _data["adj"]; $data 2[$key] [content] = $r _data["Content"]; } foreach ($data 2 as $key = = $value) { foreach ($value ["content"] as $k = + $v) { if (!empty ($v)) { Preg_match_all ("/\ \ (? P . *) \<\/p\> (? P . *) \<\/li>/us ", $v, $v _data); foreach ($v _data[' title '] as $m = = $d) { $data = Strip_tags (Preg_replace ("<>", "", $v _data["value" [$m])); $result [$key] [$value ["Adj"] [$k]][$d] = $data; } } } } return $result; } /** * Get phrase phrases * Return Array (key = value) One-dimensional or multidimensional array * */ Private Function Getphrase () { $num = self:: $num; $data = $this-GetContent (); Preg_match_all ("/id=\" en\-phrase\ "\> (. *) \/us", $data, $phrase); $data = Explode ("", $phrase [1][0]); $data 1 = array_slice ($data, 0, $num); $result = Array (); foreach ($data 1 as $key = = $value) { $data 2 = Explode (" ", $value); $n = count ($data 2); if ($n <=3) { $result [Str_replace ("," "", Strip_tags ($data 2[0])] = Strip_tags ($data 2[1]); }else{ $data 3 = array_slice ($data 2,0, $n-1); $data 4 = array_slice ($data 2,0,2); $res = Array_diff ($data 3, $data 4); $data 5 = Array_chunk ($res, 2); $key _value = Trim (Str_replace ("", "", Strip_tags ($data 4[0])); $result [$key _value] = strip_tags ($data 4[1]); foreach ($data 5 as $key = = $value) { foreach ($value as $k = = $v) { $value [$k] = Strip_tags ($v); } $array = Array ($result [$key _value], $value); if (array_key_exists ($key _value, $result)) { $result [$key _value] = $array; } } } } return $result; } /** * Convert an array to a string * * @param array $data arrays * @param bool $isformdata If 0, do not use new_stripslashes processing, optional parameter, default is 1 * @return string is returned, if data is empty, NULL is returned */ Private Function Array2string ($data, $isformdata = 1) { if ($data = = ") return"; if ($isformdata) $data = $this->new_stripslashes ($data); Return Addslashes (Var_export ($data, TRUE)); } /** * Returns a string or array that has been processed by stripslashes * @param $string string or array to be processed * @return Mixed */ Private Function New_stripslashes ($string) { if (!is_array ($string)) return stripslashes ($string); foreach ($string as $key = + $val) $string [$key] = $this->new_stripslashes ($val); return $string; } } $word = new Dict ("Express"); $word->content (); |