人人商城

translate.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. $message = $this->message;
  3. $ret = preg_match('/@(.+)/', $this->message['content'], $matchs);
  4. if(!$ret) {
  5. return $this->respText('请输入合适的格式, @查询内容(中文或英文)');
  6. }
  7. $word = $matchs[1];
  8. $url = 'http://dict.youdao.com/search?q=%s';
  9. $url = sprintf($url, $word);
  10. $resp = ihttp_get($url);
  11. if ($resp['code'] == 200 && $resp['content']) {
  12. if(preg_match('/(?P<block><h2 class="wordbook-js">.+<\/h2>)/s', $resp['content'], $block) && preg_match('/<div class="trans-container">.*?(?P<trans><ul>.+?<\/ul>).*?<\/div>/s', $resp['content'], $trans)) {
  13. $block = $block['block'];
  14. if(preg_match('/<span class="keyword">(?P<keyword>.+?)<\/span>/', $block, $keyword)) {
  15. $keyword = $keyword['keyword'];
  16. $rs = array();
  17. $ds = array();
  18. if(preg_match_all('/<span class="(pronounce|phonetic)">(?P<tic>.+?)<\/span>/s', $block, $tics)) {
  19. foreach($tics['tic'] as $line) {
  20. $line = trim(strip_tags($line));
  21. if($line) {
  22. $rs[] = preg_replace('/\s+/', ' ', $line);
  23. }
  24. }
  25. }
  26. $trans = $trans['trans'];
  27. if(preg_match_all('/<p class="wordGroup">(?P<line1>.+?)<\/p>|<li>(?P<line2>.+?)<\/li>/s', $trans, $lines)) {
  28. foreach($lines['line1'] as $line) {
  29. $line = trim(strip_tags($line));
  30. if($line) {
  31. $ds[] = $line;
  32. }
  33. }
  34. foreach($lines['line2'] as $line) {
  35. $line = trim(strip_tags($line));
  36. if($line) {
  37. $ds[] = $line;
  38. }
  39. }
  40. }
  41. $reply = "{$keyword}\n==================\n";
  42. if($rs) {
  43. $reply .= "发音:\n";
  44. foreach($rs as $row) {
  45. $reply .= "{$row}\n";
  46. }
  47. $reply .= "==================\n";
  48. }
  49. if($rs) {
  50. $ds = preg_replace("/\s+/", "", $ds);
  51. foreach($ds as $row) {
  52. $reply .= "{$row}\n";
  53. }
  54. }
  55. return $this->respText($reply);
  56. }
  57. }
  58. }
  59. return $this->respText('没有找到结果, 要不换个词试试?');