如何通过id PHP DOM获取所有元素

时间:2022-10-27 11:41:53

I've tried to print using looping but it is not printing the next element.
Please give me a simple example which explains best. using PHP DOM
Secondly can I get some Xth element which has that id.

我试图使用循环打印,但它不打印下一个元素。请给我一个简单的例子,它解释得最好。使用PHP DOM其次,我可以得到一些具有该ID的第X个元素。

update

更新

Thanks. I got error when creating elements with same id so changed it to class (sorry am newbie to programming, thanks for uplifting me). So can you please let me know how I can extract all the elements those which are having same class name and Secondly can I get some Xth element from the document which has that class name.

谢谢。我在创建具有相同id的元素时遇到错误,因此将其更改为类(对不起,我是编程的新手,感谢振作起来)。那么请你告诉我如何提取所有具有相同类名的元素,然后我可以从具有该类名的文档中获取一些Xth元素。

3 个解决方案

#1


5  

If you have two or more elements with the same id in HTML, you will receive error like this:

如果HTML中有两个或多个具有相同ID的元素,则会收到如下错误:

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID myid already defined in Entity

警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:已在实体中定义的ID myid

Because you cannot have more than one element with the same id. If you have only one:

因为您不能拥有多个具有相同ID的元素。如果你只有一个:

$dom = new DOMDocument();
$dom->loadHTML('
    <html>
        <body>
            <div></div>
            <div></div>
            <div id="myid">myid1</div>
        </body>
    </html>
');

$element = $dom->getElementById('myid');
echo $element->nodeValue."<br>";

Also, you can use DOMDocument::getElementsByTagName or XPath

此外,您可以使用DOMDocument :: getElementsByTagName或XPath

#2


2  

use Simple HTML DOM Parser

使用简单的HTML DOM解析器

$html = file_get_html('index.php');

// Find all images
foreach($html->find('div') as $element)
   echo $element->id. '<br>';

or specific div

或特定的div

// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]'); 

#3


0  

refer this link

参考此链接

u may use this
http://uk.php.net/manual/en/domnodelist.item.php

你可以使用这个http://uk.php.net/manual/en/domnodelist.item.php

$node = $parentnode->firstChild;
 do {
// do something
} while ($node = $node->nextSibling);


or this

或这个

for ($i = 0; $i < $nodeList->length; ++$i) {
    $nodeName = $nodeList->item($i)->nodeName;
    $nodeValue = $nodeList->item($i)->nodeValue;
}

#1


5  

If you have two or more elements with the same id in HTML, you will receive error like this:

如果HTML中有两个或多个具有相同ID的元素,则会收到如下错误:

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID myid already defined in Entity

警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:已在实体中定义的ID myid

Because you cannot have more than one element with the same id. If you have only one:

因为您不能拥有多个具有相同ID的元素。如果你只有一个:

$dom = new DOMDocument();
$dom->loadHTML('
    <html>
        <body>
            <div></div>
            <div></div>
            <div id="myid">myid1</div>
        </body>
    </html>
');

$element = $dom->getElementById('myid');
echo $element->nodeValue."<br>";

Also, you can use DOMDocument::getElementsByTagName or XPath

此外,您可以使用DOMDocument :: getElementsByTagName或XPath

#2


2  

use Simple HTML DOM Parser

使用简单的HTML DOM解析器

$html = file_get_html('index.php');

// Find all images
foreach($html->find('div') as $element)
   echo $element->id. '<br>';

or specific div

或特定的div

// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]'); 

#3


0  

refer this link

参考此链接

u may use this
http://uk.php.net/manual/en/domnodelist.item.php

你可以使用这个http://uk.php.net/manual/en/domnodelist.item.php

$node = $parentnode->firstChild;
 do {
// do something
} while ($node = $node->nextSibling);


or this

或这个

for ($i = 0; $i < $nodeList->length; ++$i) {
    $nodeName = $nodeList->item($i)->nodeName;
    $nodeValue = $nodeList->item($i)->nodeValue;
}