PHP简单的HTML DOM解析器

时间:2010-09-28 20:28:39

标签: php parsing dom

为什么不能使用Class的遍历方法和变量来设置参数

例如使用此数组:

array(0 => array('element' => 'img[src=images/more.gif]', 'attribute' => 'parent()->href'));

这有效:

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
{
 $this->store[$keyz] = $found->parent()->href
}

但这不是:

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
    {
     $this->store[$keyz] = $found->$target[$key]['attribute'];
    }

我试过像这样更改它们的数组:

array(0 => array('element' => 'img[src=images/more.gif]', 'traverse' => 'parent()', 'attribute' => 'href')

然后尝试:

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
        {
         $this->store[$keyz] = $found->$target[$key]['traverse']->$target[$key]['attribute'];
        }

不起作用。

在两次失败时,print_r($ this-> store)只给出了Array();

2 个答案:

答案 0 :(得分:1)

这与那个缓慢的简单HTML事件无关:这不是PHP的工作原理,你的字符串parent()->href不会被解释为对这些元素的调用。如果你需要这个,你就是在正确的轨道上,但你必须区分功能和atributes。有人认为:

array(
  'element' => 'img[src=images/more.gif]',
  'traverse' => array(
     array('parent','function'),
     array('attribute' ,'property');
...

$result = $found
foreach($target[$key]['traverse'] as $step){
   switch($step[1]){
       case 'function':
           $function = $step[0];
           $result = $found->$function();
           break;
       case 'property':
           $property = $step[0];
           $result = $found->$property;
           break;
       default:
           trigger_error("Unknown step method ".$step[1].": not an property or function",E_USER_ERROR);
   }
}
$this->store[$keyz] = $result;

或者这可以使用原始字符串:

array(
   'element' => 'img[src=images/more.gif]',
   'attribute' => 'parent()->href'));

...
$result = $found;
foreach(explode('->',$target[$key]['attribute']) as $step){
    if(substr($step,-2) == '()'){
        $function = substr($step,0, strlen($step)-2);
        $result = $result->$function();
    } else {
        $result = $result->$step;
    }
}
$this->store[$keyz] = $result;

答案 1 :(得分:0)

谢谢Wrikken,根据您的建议,我想出了这个,它完美无缺

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
            {
             if (!isset($target[$key]['traverse']) && !isset($target[$key]['attribute']))
             {
             $this->store[] = $found;
             }
             else if (isset($target[$key]['traverse']) && !isset($target[$key]['attribute']) 
             {
            $function = $target[$key]['traverse'];
            $this->store[] = $found->$function();            
           }
             else if (isset($target[$key]['traverse']) && isset($target[$key]['attribute']))
             {
              $function = $target[$key]['traverse'];
            $this->store[] = $found->$function()->$target[$key]['attribute'];       
             }
            }
         }