使用 PHP 获取 DOM 元素

发布于 2024-12-15 18:10:48 字数 1819 浏览 2 评论 0原文

我正在努力理解如何在 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 "Attribute '$name' :: '$value'<br />";
}

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

编辑(我基于评论的位置):

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

<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">

这是我收到的错误消息:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2024-12-22 18:10:49

如果重定向来自外部服务器,您将无法访问 HTML。让我这样说:在您尝试解析 DOM 时,该 DOM 并不存在。您可以做的是将文本传递给 DOM 解析器,然后以这种方式操作元素。或者更好的方法是将其添加为另一个 GET 变量。

编辑:您是否也知道客户端可以更改 HTML 并让它传递他们想要的任何内容? (使用Firebug等工具)

You won't have access to the HTML if the redirect is from an external server. Let me put it this way: the DOM does not exist at the point you are trying to parse it. What you can do is pass the text to a DOM parser and then manipulate the elements that way. Or the better way would be to add it as another GET variable.

EDIT: Are you also aware that the client can change the HTML and have it pass whatever they want? (Using a tool like Firebug)

时光倒影 2024-12-22 18:10:48

getElementsByTagName 返回一个元素列表,因此首先需要循环遍历元素,然后遍历它们的属性。

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

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

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

然后获取您的属性:

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

编辑$dom->loadHTML 只是读取文件的内容,并不执行它们。 index.php 不会以这种方式运行。你可能需要做类似的事情:

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

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'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文