Core.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Spider\Lib\Doc;
  3. use Dever;
  4. use Spider\Lib\Doc as Doc;
  5. class Core
  6. {
  7. public static function match($pattern, $string)
  8. {
  9. $temp = explode('||', $pattern);
  10. $index = isset($temp[1]) ? $temp[1] : 1;
  11. preg_match_all('/' . $temp[0] . '/i', $string, $match);
  12. $result = '';
  13. if (isset($match[$index][0])) {
  14. $result = $match[$index][0];
  15. }
  16. return $result;
  17. }
  18. public static function rule($doc, $data, $col, $rule, $key)
  19. {
  20. $result = '';
  21. $rule = Dever::split($rule);
  22. if (isset($rule[0]) && $rule[0]) {
  23. if (isset($col[$rule[0]]) && $rule[0] != $key) {
  24. if (isset($rule[1]) && $rule[1]) {
  25. $result = Doc::getInstance($doc->getUrl($data, $col, $col[$rule[0]]), $rule[1])->get();
  26. array_shift($rule);
  27. } else {
  28. $result = self::rule($doc, $data, $col, $col[$rule[0]]['collect_rule'], $key);
  29. }
  30. } else {
  31. if (is_string($data)) {
  32. $data = json_decode($data, true);
  33. }
  34. if (is_object($data)) {
  35. $result = Dom::find($data, $rule[0]);
  36. } elseif (is_array($data)) {
  37. $temp = explode('.', $rule[0]);
  38. $result = $data;
  39. foreach ($temp as $k => $v) {
  40. if (isset($result[$v])) {
  41. $result = $result[$v];
  42. }
  43. }
  44. }
  45. }
  46. }
  47. /*
  48. if (isset($rule[0]) && $rule[0]) {
  49. if (isset($col[$rule[0]]) && $rule[0] != $key) {
  50. # 此处开task
  51. if (isset($rule[1]) && $rule[1]) {
  52. $result = Doc::getInstance($doc->getUrl($data, $col, $col[$rule[0]]), $rule[1])->get();
  53. array_shift($rule);
  54. } else {
  55. $result = self::rule($doc, $data, $col, $col[$rule[0]]['collect_rule'], $key);
  56. }
  57. } elseif (isset($data[$rule[0]])) {
  58. $result = $data[$rule[0]];
  59. } else {
  60. $result = Dom::find($data, $rule[0]);
  61. }
  62. }
  63. */
  64. if (isset($rule[1]) && $rule[1]) $result = self::match($rule[1], $result);
  65. return $result;
  66. }
  67. }