12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace Spider\Lib\Doc;
- use Dever;
- use Spider\Lib\Doc as Doc;
- class Core
- {
- public static function match($pattern, $string)
- {
- $temp = explode('||', $pattern);
- $index = isset($temp[1]) ? $temp[1] : 1;
- preg_match_all('/' . $temp[0] . '/i', $string, $match);
- $result = '';
- if (isset($match[$index][0])) {
- $result = $match[$index][0];
- }
-
- return $result;
- }
- public static function rule($doc, $data, $col, $rule, $key)
- {
- $result = '';
- $rule = Dever::split($rule);
- if (isset($rule[0]) && $rule[0]) {
- if (isset($col[$rule[0]]) && $rule[0] != $key) {
- if (isset($rule[1]) && $rule[1]) {
- $result = Doc::getInstance($doc->getUrl($data, $col, $col[$rule[0]]), $rule[1])->get();
- array_shift($rule);
- } else {
- $result = self::rule($doc, $data, $col, $col[$rule[0]]['collect_rule'], $key);
- }
- } else {
- if (is_string($data)) {
- $data = json_decode($data, true);
- }
- if (is_object($data)) {
- $result = Dom::find($data, $rule[0]);
- } elseif (is_array($data)) {
- $temp = explode('.', $rule[0]);
- $result = $data;
- foreach ($temp as $k => $v) {
- if (isset($result[$v])) {
- $result = $result[$v];
- }
- }
- }
- }
- }
- /*
- if (isset($rule[0]) && $rule[0]) {
- if (isset($col[$rule[0]]) && $rule[0] != $key) {
- # 此处开task
- if (isset($rule[1]) && $rule[1]) {
- $result = Doc::getInstance($doc->getUrl($data, $col, $col[$rule[0]]), $rule[1])->get();
- array_shift($rule);
- } else {
- $result = self::rule($doc, $data, $col, $col[$rule[0]]['collect_rule'], $key);
- }
- } elseif (isset($data[$rule[0]])) {
- $result = $data[$rule[0]];
- } else {
- $result = Dom::find($data, $rule[0]);
- }
- }
- */
- if (isset($rule[1]) && $rule[1]) $result = self::match($rule[1], $result);
-
- return $result;
- }
- }
|