[英]PHP Simple HTML DOM Parser
How come you can't use the Class's traversing methods with variable's to set the parameters 为什么不能使用带有变量的Class的遍历方法来设置参数
For instance Using this array: 例如,使用此数组:
array(0 => array('element' => 'img[src=images/more.gif]', 'attribute' => 'parent()->href'));
this works: 这有效:
foreach($this->contents->find($target[$key]['element']) as $keyz => $found)
{
$this->store[$keyz] = $found->parent()->href
}
But this doesn't: 但这不是:
foreach($this->contents->find($target[$key]['element']) as $keyz => $found)
{
$this->store[$keyz] = $found->$target[$key]['attribute'];
}
I have tried changing they array like so: 我试图像这样更改它们的数组:
array(0 => array('element' => 'img[src=images/more.gif]', 'traverse' => 'parent()', 'attribute' => 'href')
And then trying: 然后尝试:
foreach($this->contents->find($target[$key]['element']) as $keyz => $found)
{
$this->store[$keyz] = $found->$target[$key]['traverse']->$target[$key]['attribute'];
}
doesn't work. 不起作用。
On both failures a print_r($this->store) simply gives Array(); 在这两次失败中,print_r($ this-> store)都只给出Array();。
It's got nothing to do with that slow Simple HTML thingamyig: this is not how PHP works, your string parent()->href
won't be interpreted as a call to those elements. 它与缓慢的简单HTML无关,它与PHP无关:PHP不是这样工作的,您的字符串parent()->href
不会被解释为对这些元素的调用。 If you need this, you are on the right track, but you have to distinguish functions & atributes. 如果需要,您将走上正确的道路,但必须区分功能和属性。 Somethink like either: 像这样的任何一种:
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;
Or this could work with your original strings: 或者这可以与您的原始字符串一起使用:
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;
Thank you Wrikken, based on your advice I came up with this, which works perfectly 谢谢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'];
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.