我想使用simple_html_dom.php从HTML文档中删除空段落。我知道如何使用DOMDocument类来实现它,但是,因为我使用的HTML文件是在MS Word中编写的,所以DOMDocument的loadHTMLFile()函数给出了这个异常“命名空间未定义”。
这是我使用DOMDocument对象的代码,用于未在MS Word中准备的HTML文件:
<?php
/* Using the DOMDocument class */
/* Create a new DOMDocument object. */
$html = new DOMDocument("1.0", "UTF-8");
/* Load HTML code from an HTML file into the DOMDocument. */
$html->loadHTMLFile("HTML File With Empty Paragraphs.html");
/* Assign all the <p> elements into the $pars DOMNodeList object. */
$pars = $html->getElementsByTagName("p");
echo "The initial number of paragraphs is " . $pars->length . ".<br />";
/* The trim() function is used to remove leading and trailing spaces as well as
* newline characters. */
for ($i = 0; $i < $pars->length; $i++){
if (trim($pars->item($i)->textContent) == ""){
$pars->item($i)->parentNode->removeChild($pars->item($i));
$i--;
}
}
echo "The final number of paragraphs is " . $pars->length . ".<br />";
// Write the HTML code back into an HTML file.
$html->saveHTMLFile("HTML File WithOut Empty Paragraphs.html");
?>
这是我使用simple_html_dom.php模块为MS Word编写的HTML文件的代码:
<?php
/* Using simple_html_dom.php */
include("simple_html_dom.php");
$html = file_get_html("HTML File With Empty Paragraphs.html");
$pars = $html->find("p");
for ($i = 0; $i < count($pars); $i++) {
if (trim($pars[$i]->plaintext) == "") {
unset($pars[$i]);
$i--;
}
}
$html->save("HTML File without Empty Paragraphs.html");
?>
它几乎相同,只是在使用DOMDocument时使用$ pars变量是一个DOMNodeList,而在使用simple_html_dom.php时是一个数组。但是这段代码不起作用。首先它运行两分钟然后报告这些错误:“Undefined offset:1”和“试图获得nonobject的属性”这一行:“if(trim($ pars [$ i] - &gt; plaintext)==” “){”。
有谁知道如何解决这个问题?
谢谢。
我也在php devnetwork询问。
答案 0 :(得分:2)
查看Simple HTML DOM Parser的文档,我认为这应该可以解决问题:
include('simple_html_dom.php');
$html = file_get_html('HTML File With Empty Paragraphs.html');
$pars = $html->find('p');
foreach($pars as $par)
{
if(trim($par->plaintext) == '')
{
// Remove an element, set it's outertext as an empty string
$par->outertext = '';
}
}
$html->save('HTML File without Empty Paragraphs.html');
我做了一个快速测试,这对我有用:
include('simple_html_dom.php');
$html = str_get_html('<html><body><h1>Test</h1><p></p><p>Test</p></body></html>');
$pars = $html->find("p");
foreach($pars as $par)
{
if(trim($par->plaintext) == '')
{
$par->outertext = '';
}
}
echo $html;
// Output: <html><body><h1>Test</h1><p>Test</p></body></html>
答案 1 :(得分:0)
空段落看起来像<p [attributes]> [spaces or newlines] </p>
(不区分大小写)。您可以使用preg_replace(或str_replace)删除空段落。
以下内容仅在空段落为<p></p>
时才有效:
$oldHtml = file_get_contents('File With Empty Paragraphs.html');
$newHtml = str_replace('<p></p>', '', $oldHtml);
// and write the new HTML to the file
$fh = fopen('File Without Empty Paragraphs.html', 'w');
fwrite($fh, $newHtml);
fclose($fh);
这也适用于具有属性的段落,例如<p class="msoNormal"> </p>
:
$oldHtml = file_get_contents('File With Empty Paragraphs.html');
$newHtml = preg_replace('#<p[^>]*>\s*</p>#i', '', $oldHtml);
// and write the new HTML to the file
$fh = fopen('File Without Empty Paragraphs.html', 'w');
fwrite($fh, $newHtml);
fclose($fh);