Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php DomDocument获取所有div并放入数组中_Php_Domdocument - Fatal编程技术网

Php DomDocument获取所有div并放入数组中

Php DomDocument获取所有div并放入数组中,php,domdocument,Php,Domdocument,我有一些div具有相同的Id和相同的Class,如下所示: <div id="results_information" class="control_results"> <!-- I have divs, subDivs, span, images inside --> </div> <div id="results_information" class="control_results"> <!-- I have divs, subDiv

我有一些div具有相同的
Id
和相同的
Class
,如下所示:

<div id="results_information" class="control_results">
<!-- I have divs, subDivs, span, images inside -->
</div>

<div id="results_information" class="control_results">
<!-- I have divs, subDivs, span, images inside -->
</div>

....
但是它不起作用,我如何解决这个问题并将div放入数组中?

您需要使用类名来获取元素

$dom = new DOMDocument(); 
$xpath = new DOMXpath($dom);
$div = $xpath->query('//div[contains(@class, "control_results")]')
您将需要使用类名来使用和获取元素

$dom = new DOMDocument(); 
$xpath = new DOMXpath($dom);
$div = $xpath->query('//div[contains(@class, "control_results")]')

要解决您的问题,您需要执行以下步骤:

首先,您应该基于选择
类而不是
ID
(因为在这种情况下
ID
应该是唯一的)

在这种情况下,我们假设在名为
$htmlOut
的变量中包含以下html:

<div id="results_information" class="control_results">
<span style="background:black; color:white">
hellow world
</span>
<strong>2</strong>
</div>

<div id="results_information" class="control_results">
<strong>2</strong>
<img src="hello.png" />
</div>
使用该代码,我们可以提取具有classname
control\u results
的div的所有内容,并将其放入变量
$nodes

现在我们需要解析变量
$nodes
(这是一个数组)并提取这两个类的所有HTML。为此,我创建了一个函数来处理:

function get_inner_html( $node ) { 
    $innerHTML= ''; 
    $children = $node->childNodes; 
    foreach ($children as $child) { 
        $innerHTML .= $child->ownerDocument->saveXML( $child ); 
    } 

    return $innerHTML;  
}  
此函数将提取每个子节点(类
control\u results
中的每个HTML代码)并返回

现在,您只需要为变量$nodes创建一个foreach并调用该函数,如下所示:

foreach ($nodes as $rowNode) {
    $array[] = get_inner_html($rowNode);
}

var_dump($array);
以下是完整的代码:

$htmlOut = '
<div id="results_information" class="control_results">
<span style="background:black; color:white">
hellow world
</span>
<strong>2</strong>
</div>

<div id="results_information" class="control_results">
<strong>2</strong>
<img src="hello.png" />
</div>
';

$array = array();
$dom = new DomDocument();
$dom->loadHtml($htmlOut);
$finder = new DomXPath($dom);
$classname = "control_results";
$nodes = $finder->query("//*[contains(@class, '$classname')]");

foreach ($nodes as $rowNode) {
    $array[] = get_inner_html($rowNode);
}

var_dump($array);


function get_inner_html( $node ) { 
    $innerHTML= ''; 
    $children = $node->childNodes; 
    foreach ($children as $child) { 
        $innerHTML .= $child->ownerDocument->saveXML( $child ); 
    } 

    return $innerHTML;  
}  
$htmlOut='1〕
地狱世界
2
2
';
$array=array();
$dom=新的DomDocument();
$dom->loadHtml($htmlOut);
$finder=newdomxpath($dom);
$classname=“控制结果”;
$nodes=$finder->query(“/*[包含(@class,$classname')]”);
foreach($rowNode作为$node的节点){
$array[]=获取内部html($rowNode);
}
变量转储($数组);
函数get_internal_html($node){
$innerHTML='';
$children=$node->childNodes;
foreach($childrenas$child){
$innerHTML.=$child->ownerDocument->saveXML($child);
} 
返回$innerHTML;
}  
但这段代码有一个小问题,如果您检查数组中的结果是:

 0 => string '<span style="background:black; color:white">hellow world</span><strong>2</strong>',

 1 => string '<strong>2</strong><img src="hello.png"/>'
0=>字符串“hellow world2”,
1=>字符串“2
而不是:

 0 => string '<div id="results_information" class="control_results"><span style="background:black; color:white">hellow world</span><strong>2</strong></div>',

 1 => string '<div id="results_information" class="control_results"><strong>2</strong><img src="hello.png"/></div>'
0=>字符串“hellow world2”,
1=>字符串“2

在这种情况下,您可以执行此数组的foreach并将该div包含在内容的init中,然后在内容的final中关闭该div并重新保存该数组。

要解决此问题,您需要执行以下步骤:

首先,您应该基于选择
类而不是
ID
(因为在这种情况下
ID
应该是唯一的)

在这种情况下,我们假设在名为
$htmlOut
的变量中包含以下html:

<div id="results_information" class="control_results">
<span style="background:black; color:white">
hellow world
</span>
<strong>2</strong>
</div>

<div id="results_information" class="control_results">
<strong>2</strong>
<img src="hello.png" />
</div>
使用该代码,我们可以提取具有classname
control\u results
的div的所有内容,并将其放入变量
$nodes

现在我们需要解析变量
$nodes
(这是一个数组)并提取这两个类的所有HTML。为此,我创建了一个函数来处理:

function get_inner_html( $node ) { 
    $innerHTML= ''; 
    $children = $node->childNodes; 
    foreach ($children as $child) { 
        $innerHTML .= $child->ownerDocument->saveXML( $child ); 
    } 

    return $innerHTML;  
}  
此函数将提取每个子节点(类
control\u results
中的每个HTML代码)并返回

现在,您只需要为变量$nodes创建一个foreach并调用该函数,如下所示:

foreach ($nodes as $rowNode) {
    $array[] = get_inner_html($rowNode);
}

var_dump($array);
以下是完整的代码:

$htmlOut = '
<div id="results_information" class="control_results">
<span style="background:black; color:white">
hellow world
</span>
<strong>2</strong>
</div>

<div id="results_information" class="control_results">
<strong>2</strong>
<img src="hello.png" />
</div>
';

$array = array();
$dom = new DomDocument();
$dom->loadHtml($htmlOut);
$finder = new DomXPath($dom);
$classname = "control_results";
$nodes = $finder->query("//*[contains(@class, '$classname')]");

foreach ($nodes as $rowNode) {
    $array[] = get_inner_html($rowNode);
}

var_dump($array);


function get_inner_html( $node ) { 
    $innerHTML= ''; 
    $children = $node->childNodes; 
    foreach ($children as $child) { 
        $innerHTML .= $child->ownerDocument->saveXML( $child ); 
    } 

    return $innerHTML;  
}  
$htmlOut='1〕
地狱世界
2
2
';
$array=array();
$dom=新的DomDocument();
$dom->loadHtml($htmlOut);
$finder=newdomxpath($dom);
$classname=“控制结果”;
$nodes=$finder->query(“/*[包含(@class,$classname')]”);
foreach($rowNode作为$node的节点){
$array[]=获取内部html($rowNode);
}
变量转储($数组);
函数get_internal_html($node){
$innerHTML='';
$children=$node->childNodes;
foreach($childrenas$child){
$innerHTML.=$child->ownerDocument->saveXML($child);
} 
返回$innerHTML;
}  
但这段代码有一个小问题,如果您检查数组中的结果是:

 0 => string '<span style="background:black; color:white">hellow world</span><strong>2</strong>',

 1 => string '<strong>2</strong><img src="hello.png"/>'
0=>字符串“hellow world2”,
1=>字符串“2
而不是:

 0 => string '<div id="results_information" class="control_results"><span style="background:black; color:white">hellow world</span><strong>2</strong></div>',

 1 => string '<div id="results_information" class="control_results"><strong>2</strong><img src="hello.png"/></div>'
0=>字符串“hellow world2”,
1=>字符串“2

在这种情况下,您可以执行此数组的foreach,并在内容的init中包含该div,在内容的final中关闭该div,然后重新保存该数组。

Id属性应该是唯一的。您需要使用类来选择目标divsId属性应该是唯一的。您需要使用类来选择目标div