PHP production Baidu Dictionary search word collector _php Tutorial

Source: Internet
Author: User
Tags array example

PHP production Baidu Dictionary search word collection device


This article mainly introduces the PHP production Baidu Dictionary Search Word collection of relevant information, the need for friends can refer to the next

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 ~

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

/**

* 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 ();

The above is the whole content of this article, very practical features, I hope that small partners can like.

http://www.bkjia.com/PHPjc/950763.html www.bkjia.com true http://www.bkjia.com/PHPjc/950763.html techarticle PHP production Baidu dictionary Search word collector This article mainly introduces the PHP production Baidu Dictionary Search Word collection of relevant data, the need for friends can refer to the following Baidu Dict Collection sample Write ...

  • 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.