PHP production Baidu Dictionary search word collector, php Baidu dictionary collector _php Tutorial

Source: Internet
Author: User
Tags array example

PHP production Baidu Dictionary search word collector, PHP Baidu Dictionary collection device


Baidu Dict Collection Sample

Write the collection of Baidu Dict dictionary after the translation of all the results of the data, of course, with the 13.5w Word Library and collection of simple cases, here I write the main class dict.class.php out, project address Http://github.com/widuu/baidu_ Dict, there is a need for direct fork to be able to ~ the DA, this thing with few people, so useful brother took the HA ~

<?php/** * dict.class.php collect Baidu Dictionary translation content * * @copyright (C) widuu * @license http://www.widuu.com * @lastmo  Dify 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 Baidu method of collecting data * @param string English word * Retun Array (*symbol "= transcription *" Pro " + Pronunciation * "example" = "explain" + * * * * * * * * "synonym" = same antonym * "phrase" = phrase array *) * */public function Conten T ($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,//pronounce "example" = = $example,//"explain" +/= $explain,// Concise interpretation of "synonym" and $synonym,//the same antonym "phrase" = + $phrase//phrases array); return $result;} /** * Remote access 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), $result = Curl_exec ($ch), if (Curl_errno ($curl)) {echo ' errno '. Curl_error ($curl) ;} Curl_close ($ch); return $result;} /** * Get Baidu Translate pronunciation * retun Array (UK, US) * */private function pronounced () {$data = $this, getcontent ();p Reg_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 ();p reg_match_all ("/urL=\ "(. *) \"/ui ", $data, $pronounced); return array (' en ' = = $pronounced [1][0], ' us ' = = $pronounced [1][1]);} /** * Get Baidu Translation Example * return array () multidimensional Array example * */private function getexample () {$str = ""; $data = $this, GetContent (  );p Reg_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 Concise explanation * return Array (x = "Part of speech", B = "accessory") * **/private function Getexplain () {$data = $this-GetC Ontent ();p reg_match_all ("/id\=\" en\-simple\-means\ "\> (. *) \/us", $data, $explain); $r _data = $explain [1][0];p Reg_ Match_all ("/\
 
  \
  
   (? p.*) \<\/strong\>\
   
    (? P
    
     . *) \<\/span\>\<\/p\>/us ", $r _data, $a _data);p reg_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") generally multidimensional array * */private function getsynonym () {$data = $this, get Content ();p reg_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
             <title>. *) \<\/p\> (? P</title>
             
              . *) \<\/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 phrase * return array (key = value) One-dimensional or multidimensional array * */private function getphrase () {$num = self:: $num; $data = $this-& Gt GetContent ();p reg_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 = expl Ode ("
              

", $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 ($ Data4[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;} /** * Converts an array to a string * * @param array $data array * @param bool $isformdata if 0, new_stripslashes processing is not used, optional parameter, default = 1 * @return string Returns the string, if data is empty, returns an empty */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 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 the whole content of this article, very practical features, I hope that small partners can like.

http://www.bkjia.com/PHPjc/949449.html www.bkjia.com true http://www.bkjia.com/PHPjc/949449.html techarticle PHP production Baidu dictionary search word collector, php Baidu dictionary collector Baidu Dict collected samples of the collection of Baidu Dict dictionary after the translation of all the results of the data, of course, with the 13.5w Word Library and mining ...

  • Related Article

    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.