使用 PHP 获取 DOM 元素 [英] Using PHP to get DOM Element

查看:35
本文介绍了使用 PHP 获取 DOM 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解如何在 PHP 中使用 DOMElement 对象.我找到了这段代码,但我不确定它是否适用于我:

$dom = new DOMDocument();$dom->loadHTML("index.php");$div = $dom->getElementsByTagName('div');foreach ($div->attributes as $attr) {$name = $attr->nodeName;$value = $attr->nodeValue;echo "属性 '$name' :: '$value'<br/>";}

基本上我需要的是在 DOM 中搜索具有特定 idelement,然后我需要提取一个非标准的 attribute(即我用 JS 编写并穿上的那个)所以我可以看到它的价值.原因是我需要 $_GET 中的一部分和基于重定向的 HTML 中的一部分.如果有人可以解释我如何为此目的使用 DOMDocument,那会很有帮助.我真的很难理解正在发生的事情以及如何正确实施它,因为我显然做得不对.

编辑(根据评论我所处的位置):

这是我的代码第 4-26 行供参考:

<?phprequire_once($_SERVER["DOCUMENT_ROOT"] ."/peripheral/profile.php");$searchResults = isset($_GET["s"]) ?performSearch($_GET["s"]) : "";$dom = 新的 DOMDocument();$dom->load("index.php");$divs = $dom->getElementsByTagName('div');foreach ($divs 作为 $div) {foreach ($div->attributes as $attr) {$name = $attr->nodeName;$value = $attr->nodeValue;echo "属性 '$name' :: '$value'<br/>";}}$div = $dom->getElementById('currentLocation');$attr = $div->getAttribute('srckey');echo "<h1>{$attr}</a>";?>

<div id="column_main">

这是我收到的错误消息:

警告:DOMDocument::load() [domdocument.load]:../public_html/index.php 中文档末尾的额外内容,第 26 行,../public_html/index.php在第 10 行致命错误:在第 21 行的 ../public_html/index.php 中的非对象上调用成员函数 getAttribute()

解决方案

getElementsByTagName 返回一个元素列表,所以首先你需要遍历元素,然后遍历它们的属性.

$divs = $dom->getElementsByTagName('div');foreach ($divs 作为 $div) {foreach ($div->attributes as $attr) {$name = $attr->nodeName;$value = $attr->nodeValue;echo "属性 '$name' :: '$value'<br/>";}}

就您而言,您说您需要一个特定的 ID.这些应该是唯一的,所以要做到这一点,你可以使用 (note getElementById 可能不起作用,除非您先调用 $dom->validate()):

$div = $dom->getElementById('divID');

然后获取您的属性:

$attr = $div->getAttribute('customAttr');

EDIT:$dom->loadHTML 只是读取文件的内容,不会执行它们.index.php 不会以这种方式运行.您可能需要执行以下操作:

$dom->loadHTML(file_get_contents('http://localhost/index.php'))

I'm struggling big time understanding how to use the DOMElement object in PHP. I found this code, but I'm not really sure it's applicable to me:

$dom = new DOMDocument();
$dom->loadHTML("index.php");

$div = $dom->getElementsByTagName('div');
foreach ($div->attributes as $attr) {
     $name = $attr->nodeName;
     $value = $attr->nodeValue;
     echo "Attribute '$name' :: '$value'<br />";
}

Basically what I need is to search the DOM for an element with a particular id, after which point I need to extract a non-standard attribute (i.e. one that I made up and put on with JS) so I can see the value of that. The reason is I need one piece from the $_GET and one piece that is in the HTML based from a redirect. If someone could just explain how I use DOMDocument for this purpose, that would be helpful. I'm really struggling understanding what's going on and how to properly implement it, because I clearly am not doing it right.

EDIT (Where I'm at based on comment):

This is my code lines 4-26 for reference:

<div id="column_profile">
    <?php
        require_once($_SERVER["DOCUMENT_ROOT"] . "/peripheral/profile.php");            
        $searchResults = isset($_GET["s"]) ? performSearch($_GET["s"]) : "";

        $dom = new DOMDocument();
        $dom->load("index.php");

        $divs = $dom->getElementsByTagName('div');
        foreach ($divs as $div) {
            foreach ($div->attributes as $attr) {
              $name = $attr->nodeName;
              $value = $attr->nodeValue;
              echo "Attribute '$name' :: '$value'<br />";
            }
        }
        $div = $dom->getElementById('currentLocation');
        $attr = $div->getAttribute('srckey');   
        echo "<h1>{$attr}</a>";
    ?>
</div>

<div id="column_main">

Here is the error message I'm getting:

Warning: DOMDocument::load() [domdocument.load]: Extra content at the end of the document in ../public_html/index.php, line: 26 in ../public_html/index.php on line 10

Fatal error: Call to a member function getAttribute() on a non-object in ../public_html/index.php on line 21

解决方案

getElementsByTagName returns you a list of elements, so first you need to loop through the elements, then through their attributes.

$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {
    foreach ($div->attributes as $attr) {
      $name = $attr->nodeName;
      $value = $attr->nodeValue;
      echo "Attribute '$name' :: '$value'<br />";
    }
}

In your case, you said you needed a specific ID. Those are supposed to be unique, so to do that, you can use (note getElementById might not work unless you call $dom->validate() first):

$div = $dom->getElementById('divID');

Then to get your attribute:

$attr = $div->getAttribute('customAttr');

EDIT: $dom->loadHTML just reads the contents of the file, it doesn't execute them. index.php won't be ran this way. You might have to do something like:

$dom->loadHTML(file_get_contents('http://localhost/index.php'))

这篇关于使用 PHP 获取 DOM 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆