[英]Finding and removing html tags with PHP Simple HTML DOM Parser
This is the code I am using:这是我正在使用的代码:
include 'simple_html_dom.php';
$html = file_get_html('index.html');
echo $html->find('tr', 15);
This will find the row 15 of the table.这将找到表的第 15 行。 What I want to do is to remove that row completely.
我想要做的是完全删除该行。
I have already tried我已经试过了
$html->find('tr', 15)=null;
But that does not seem to work.但这似乎不起作用。 I have tried finding the info on the SimpleHTMLDom documentation but it does no contain much information.
我曾尝试在SimpleHTMLDom 文档中查找信息,但它没有包含太多信息。
simple_html_dom does not seems to allow the deletion. simple_html_dom 似乎不允许删除。
Try with this instead:试试这个:
$html = new DOMDocument();
$html->loadHTMLFile('index.html');
$element = $html->getElementsByTagName('tr')->item(15);
$element->parentNode->removeChild($element);
here you have a working example (works as is in Linux, but is easily adaptable).在这里你有一个工作示例(在 Linux 中工作,但很容易适应)。
File dom_test.php
:文件
dom_test.php
:
#!/usr/bin/php
<?php
$html = new DOMDocument();
$html->loadHTMLFile('index.html');
$element = $html->getElementsByTagName('tr')->item(1);
$element->parentNode->removeChild($element);
echo $html->saveHTML();
?>
Where the index.html
contains:其中
index.html
包含:
<html>
<head></head>
<body>
<table>
<tr><td> hi </td><td>there</td></tr>
<tr>
<td> HELLO </td>
<td> there </td>
</tr>
<tr><td> hi </td><td>there</td></tr>
</table>
</body>
</html>
Put both files in the same directory and execute this in the console:将这两个文件放在同一目录中并在控制台中执行:
php dom_test.php
The output will appears without the "HELLO there" row.输出将不显示“HELLO there”行。
I hope that helps you.我希望这对你有帮助。
You can do this with simple_html_dom, just set the outertext to the value of innertext你可以用simple_html_dom来做到这一点,只需将outertext设置为innertext的值
foreach($html->find('div') as $div) {
$div->outertext = $div->innertext;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.