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

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

问题描述

我很努力地了解如何在PHP中使用DOMElement对象。我发现这个代码,但我并不确定它适用于我:

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

$ div = $ dom-> getElementsByTagName('div');
foreach($ div->属性为$ attr){
$ name = $ attr-> nodeName;
$ value = $ attr-> nodeValue;
echoAttribute'$ name'::'$ value'< br />;
}

基本上我需要的是搜索DOM $ 元素与特定 id ,之后我需要提取一个非标准的属性(即我编写并放在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;
echoAttribute'$ name'::'$ value'< br />;
}
}
$ div = $ dom-> getElementById('currentLocation');
$ attr = $ div-> getAttribute('srckey');
echo< h1> {$ attr}< / a>;
?>
< / div>

< div id =column_main>

以下是我收到的错误消息:

 警告:DOMDocument :: load()[domdocument.load]:文件末尾的额外内容在../public_html/index.php中,行:26 in。 ./public_html/index.php第10行

致命错误:调用第21行../public_html/index.php中的非对象的成员函数getAttribute()


解决方案

getElementsByTagName 你是一个元素列表,所以首先你需要循环遍历元素,然后通过它们的属性。

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

在你的情况下,你说你需要一个特定的ID 。那些应该是唯一的,所以要这样做,你可以使用(注意 getElementById 可能无法正常工作,除非您先调用 $ dom-> validate()

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

然后获取您的属性:

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

编辑 $ 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天全站免登陆