PHP: Baidu Dictionary query collector, php Baidu Dictionary collector _ PHP Tutorial

Source: Internet
Author: User
Tags array example
PHP makes Baidu Dictionary word query collector and php Baidu Dictionary collector. PHP makes Baidu Dictionary word query collector, php Baidu Dictionary collector Baidu dict collects samples and writes all the result data after Baidu dict dictionary translation, of course, it comes with the 13.5w word library and the Baidu Dictionary query collector made by using PHP, and the php Baidu Dictionary collector.

Samples collected by Baidu dict

The collection of all the result data after Baidu dict dictionary translation, of course, comes with the 13.5w word library and simple collection cases. here I will write the main class dict. class. php is released. the Project address is http://github.com/widuu/baidu_dict. if you have a specific direct fork, you can get it ~ Some of them are used by very few people, so some useful brothers have taken them away ~

<? Php/*** dict. class. php: collect Baidu Dictionary translation content ** @ copyright (C) 2014 widuu * @ license http://www.widuu.com * @ Lastmodify 2014-2-15 */header ("content-type: text/html; charset = utf8"); class Dict {private $ word; // private static $ num = 10; public function _ construct () {}/*** public return method for Baidu data collection * @ param string English word * retun array (* symbol "=> phonetic symbol *" pro "=> pronunciation *" example" => Example * "explain" => concise description * "synonym" => same antonym * "phrase" => phrase array *) **/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 alphabet "pro" => $ pro, // pronunciation "example" => $ example, // example sentence "explain" => $ explain, // concise description "synonym" => $ synonym, // same antonym "phrase" => $ phrase // phrase array); return $ result ;} /*** obtain Baidu translation content remotely * 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, batch, 1); curl_setopt ($ ch, CURLOPT_HEADER, 0 ); curl_setopt ($ ch, CURLOPT_TIMEOUT, 30); $ result = curl_exec ($ ch); if (curl_errno ($ curl) {echo 'er Rno '. curl_error ($ curl);} curl_close ($ ch); return $ result;}/*** get Baidu translation pronunciation * retun array (English, US) **/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 translation 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 example: * return array () multi-dimensional array example: **/private function getExample () {$ str = ""; $ data = $ this-> getContent (); preg_match_all ("/var example_data = (. *) \] \;/Us ", $ data, $ example); $ data1 = "[[[". ltrim ($ example [1] [0], "["); $ data2 = explode ("[[", $ data1 ); $ num = count (array_filter ($ data2); foreach ($ da Ta2 as $ key => $ value) {$ data3 = explode ("[[","[[". $ value); foreach ($ data3 as $ k => $ v) {preg_match_all ("/\[\"(. *) \ ",/Us ","[". $ v, $ match); if (! Empty ($ match [1]) {$ str. = implode ($ match [1], ""). "@" ;}}$ data4 = trim ($ str, "@"); $ data5 = explode ("@", $ data4 ); $ result = array_chunk ($ data5, 2); return $ result;}/*** get concise meaning * return array (x => "part of speech ", B => "affiliated") ***/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 synonym * return array (0 => "synonym", 1 => "antonym ") it is generally a multi-dimensional array **/private function getSynonym () {$ data = $ this-> getContent (); preg_match_all ("/id = \" en \-syn \-ant \ "\> (. *)
       
         /Us ", $ data, $ synonym); $ content = $ synonym [1] [0]; $ data1 = explode ("", $ Content); $ result = array (); $ data2 = array (); foreach ($ data1 as $ key => $ value) {preg_match_all ("/\
        
          (? P. *) \; \ <\/strong \> \ <\/p \> \
         
           \
          
            (?
           
             . *) \ <\/Ul \>/Us ", $ value, $ r_data); $ data2 [$ key] [" adj "] = $ r_data [" adj "]; $ data2 [$ key] ["content"] = $ r_data ["content"];} foreach ($ data2 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 the phrase * return array (key => value) one-dimensional or multi-dimensional array **/private function getPhrase () {$ num = self: $ num; $ data = $ this-> getContent (); preg_match_all ("/id = \" en \-phrase \ "\> (. *)\
               

/Us ", $ data, $ phrase); $ data = explode ("", $ Phrase [1] [0]); $ data1 = array_slice ($ data, 0, $ num); $ result = array (); foreach ($ data1 as $ key => $ value) {$ data2 = explode ("

", $ Value); $ n = count ($ data2); if ($ n <= 3) {$ result [str_replace ("","", strip_tags ($ data2 [0])] = strip_tags ($ data2 [1]);} else {$ data3 = array_slice ($ data2, 0, $ n-1 ); $ data4 = array_slice ($ data2, 0,2); $ res = array_diff ($ data3, $ data4); $ data5 = array_chunk ($ res, 2 ); $ key_value = trim (str_replace ("", "", strip_tags ($ data4 [0]); $ result [$ key_value] = strip_tags ($ data4 [1]); foreach ($ data5 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 array * @ param bool $ isformdata if it is 0, new_stripslashes is not used for processing. optional parameter, the default value is 1 * @ return string. if data is null, null */private function array2string ($ data, $ isformdata = 1) {if ($ data = '') Return''; if ($ isformdata) $ data = $ this-> new_stripslashes ($ data); return addslashes (var_export ($ data, TRUE ));} /*** return the string or array processed by stripslashes * @ param $ string the 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 ();

The above is all the content of this article. it is very practical and I hope my friends will like it.

Baidu dict acquisition sample writing collection all the result data after Baidu dict dictionary translation, of course, with a word library and collection...

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.