PHP中的HTML DOM操作 [英] HTML DOM manipulation in PHP

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

问题描述

我想知道如何使用PHP操作DOM树吗?



我已经看到了将XML DOM加载到html文件中的一些答案。但是,如果我不需要加载怎么办?如果我要处理的文档中包含XML DOM脚本怎么办?



下面有一个示例可以打印出所有文件夹。在答案中填空。我想使用文件夹名称作为文本节点创建div-elements。答案需要有一些XML DOM脚本,因为我将在网站中创建的元素不仅仅是一个div元素。例如,使用echo并不实用,因为您可能会在错误的元素等中插入一个元素。

  $ sql = 
从文件夹
中选择名称
WHERE profileId ='$ profileId'
;
$ result = mysql_query($ sql)或die(’Error6:’.mysql_error());
while($ row = mysql_fetch_array($ result)){

}


解决方案

您不需要PHP即可操纵已生成的文档的dom。到目前为止,让PHP直接生成一些HTML更加容易。 DOM可以剖析/更改在其他位置生成的HTML。换句话说,您的脚本就是:

  while($ row = mysql_fetch_array($ result)){
回声< div> {$ row ['name']}< / div>;
}






DOM示例,以进行演示



好吧,这是一种PHP方法,用于生成一段带有一些内部跨度且没有其他内容的文本段落:



<$ 。 div>;

等效的DOM调用:(未经测试,只是为了演示):

  $ dom =新DOM; 

$ bold =新的DOMElement(’b’);
$ bold-> appendChild(new DOMText(‘but this’’));

$ span =新的DOMElement(’span’);
$ span-> appendChild(new DOMText(’有很多类似的东西,’));
$ span-> appendChild($ bold);
$ span-> appendChild(new DOMText('is');

$ div $ div = new DOMElement('div');
$ div-> appendChild(new DOMText('这是我的div,'));
$ div-> appendChild($ span);
$ div-> appendChild('mine');

echo $ div-> saveXML();

所以.......使用DOM比使用 echo


更容易

I wonder how I can manipulate the DOM tree using PHP?

I have seen some answers with XML DOM that loads in a html file. But what if I don't need to load? What if have XML DOM scripts inside the document I want to manipulate?

I have an example below that prints out all the folders. Fill in the blanks in your answers. I want to create div-elements with the folder's name as text node. The answer need to have some XML DOM scripts because I will create more elements than just one div-element in my website. And using for example echo is not practical, because you might insert an element inside the wrong element etc.

$sql = "
    SELECT name
    FROM folders
    WHERE profileId = '$profileId'
";
$result = mysql_query($sql) or die('Error6: '.mysql_error());
while($row = mysql_fetch_array($result)) {

}

解决方案

You don't need PHP to manipulate the dom of a document you're generating already. It's by far easier to just have PHP directly generate some HTML. DOM's there to disect/change HTML that was generated elsewhere. In other words, your script would just be:

while($row = mysql_fetch_array($result)) {
    echo "<div>{$row['name']}</div>";
}


DOM example, to demonstrate the tediousness

Ok, here's the PHP method to generate a paragraph of text with some internal spans and whatnot:

echo "<div> This is my div, <span>There are many like it, <b>but this one</b> is</span> mine</div>";

The equivalent DOM calls: (not tested, just to demonstrate):

$dom = new DOM;

$bold = new DOMElement('b');
$bold->appendChild(new DOMText('but this one'));

$span = new DOMElement('span');
$span->appendChild(new DOMText('There are many like it,'));
$span->appendChild($bold);
$span->appendChild(new DOMText(' is');

$div = new DOMElement('div');
$div->appendChild(new DOMText(' This is my div,'));
$div->appendChild($span);
$div->appendChild(' mine');

echo $div->saveXML();

So....... still thing using DOM is easier than using echo?

这篇关于PHP中的HTML DOM操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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