PHP production Baidu Dictionary Search word collector _php Example

Source: Internet
Author: User

Baidu Dict Sample Collection

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

<?php/** * dict.class.php collection 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;

	Number of bars displayed private static $num = 10;  Public Function __construct () {}/** * Common return Baidu data collection method * @param string English word * retun Array (* symbol) => Phonetic symbols * "Pro" => pronunciation * "Example" => example * "Explain" => concise interpretation * "synonym" => the 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 "pro" => $pro,//pronunciation "example" => $example,//Example "E Xplain "=> $explain,//Concise interpretation" SynoNym "=> $synonym,//same antonym" phrase "=> $phrase//phrase 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, 30);
		$result = curl_exec ($ch);
		if (Curl_errno ($curl)) {echo ' errno '. Curl_error ($curl);
		} curl_close ($ch);
	return $result; /** * get Baidu Translation 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 Translation pronunciation * return Array (English, American) * */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 () 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 Concise explanation * return Array (x => "Part of speech", b => "subordinate") * **/Private Function Getexplain () {$data = $
		This-> getcontent ();
		Preg_match_all ("/id\=\" en\-simple\-means\ "\> (. *) \<div (\s+) class\=\" source\ "\>/us", $data, $explain);
		$r _data = $explain [1][0]; Preg_match_all ("/\<p\>\<strong\>"? p<adj>.*) \<\/strong\>\<span\> (?
		p<name>.*) \<\/span\>\<\/p\>/us ", $r _data, $a _data); Preg_match_all ("/\<span\>"? p<tag>[^\>]+) \:\<a (\s+) href\=\ "(. *) \" \> (?)
		
		p<word>.*) \<\/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") General for multidimensional Array * */Private Function getsynonym () {
		$data = $this-> getcontent ();
		Preg_match_all ("/id=\" en\-syn\-ant\ "\> (. *) <div (\s+) class\=\" source\ ">/us", $data, $synonym);
		$content = $synonym [1][0];
		$data 1 = explode ("</dl>", $content);
		$result = Array ();
		$data 2 = array (); foreach ($data 1 as $key => $value) {preg_match_all ("/\<strong\>") (? p<adj>.*) \ \;\<\/strong\>\<\/div\>\<div (\s+) class\=\ "syn\-ant\-list\" \>\<ul\
			> (<content>.*) \<\/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 ("/\<li\>\<p\>"? p<title>.*) \<\/p\> (? p<value>.*) \<\/li&Gt;/us ", $v, $v _data); foreach ($v _data[' title] as $m => $d) {$data = Strip_tags (Preg_replace ("<</a>>", ", $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-> getcontent ();
		Preg_match_all ("/id=\" en\-phrase\ "\> (. *) \<div class\=\" source\ "\>/us", $data, $phrase);
		$data = Explode ("</dd>", $phrase [1][0]);
		$data 1 = array_slice ($data, 0, $num);
		$result = Array ();
			foreach ($data 1 as $key => $value) {$data 2 = explode ("</p>", $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; /** * Converts arrays to Strings * * @param array $data arrays * @param bool $isformdata If 0, do not use new_stripslashes processing, optional parameters, default Returns a string of 1 * @return string, and if data is empty, returns the null/Private function array2string ($data, $isformdata = 1) {if ($data = = '
	  ') return ';
	  if ($isformdata) $data = $this->new_stripslashes ($data);
	Return Addslashes (Var_export ($data, TRUE)); /** * Returns stripslashes-processed string or array * @param $string string or array to be processed * @return Mixed/Private function New_stripsla
	  Shes ($string) {if (!is_array ($string)) return stripslashes ($string); foreach ($stringAs $key => $val) $string [$key] = $this->new_stripslashes ($val);
	return $string;
}//$word = new Dict ("Express"); $word->content ();

The above is the entire content of this article, very practical functions, I hope that small partners can enjoy.

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.