- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 simple_html_dom.php 从 HTML 文档中删除空段落.我知道如何使用 DOMDocument 类来做到这一点,但是,因为我使用的 HTML 文件是在 MS Word 中准备的,DOMDocument 的 loadHTMLFile() 函数给出了这个异常“命名空间未定义”。
这是我对未在 MS Word 中准备的 HTML 文件使用 DOMDocument 对象的代码:
<?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");
?>
几乎是一样的,只是$pars变量在使用DOMDocument时是一个DOMNodeList,而在使用simple_html_dom.php时是一个数组。但是这段代码不起作用。首先它运行两分钟然后报告这些错误:“Undefined offset: 1”和“Trying to get property of nonobject”这一行:“if (trim($pars[$i]->plaintext) == “” ) {".
有谁知道我该如何解决这个问题?
谢谢。
我还在 php devnetwork 上问过.
最佳答案
查看 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>
关于php - 如何使用 simple_html_dom.php 从 HTML 文件中删除空段落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3740660/
我在 PHP 中使用 simple_html_dom 来获取 html 页面中的所有 A 标签。但是一些标签在 div 中,应该被忽略。 我想我需要这样的东西: $htmldom->find("!di
我正在尝试用 simplehtmldom 解析一个 HTML 文件我收到了这个错误: zend_mm_heap corrupted 在一个 8231 行的 HTML 文件上执行大约 4 秒后。这可能是
我有以下代码: include('../scrape/simple_html_dom.php'); $file = "http://www.espn.com/golf/leaderboard?tour
我正在尝试使用 simple_html_dom 打开文件并替换 TR 属性。 该文件包含一个表,其中有几行没有分配 css 类或 id: 我使用此代码仅从加载的页面中获取表格: $html = fi
我用过PHP Simple HTML DOM Parser首先通过 str_get_html() 将 HTML 字符串转换为 DOM 对象simple_html_dom.php的方法 $summary
我使用简单的 HTML DOM 解析器通过以下代码获取图像及其 url: load_file($url); foreach($html->find('img') as $img){
我正在使用简单的 html dom 来解析 itunes 预览页面 当我将 URL 放入代码 [download]url[/download] 时一切正常,但是当我有 2 个或更多 [download
我已经使用 simple_html_dom 在 mysql 数据库中存储了几页 html,如下所示。 scraper.php 然后,如果我回显插入到数据库中的数据,我会得到准确的抓取页面。 但是如果
我试图让这两个库相互协作,我当前的代码如下所示: phantomjs.js var page = require('webpage').create(); var system = require('
我正在使用 Appcelerator Titanium 构建一个 iPhone 应用程序,我需要将一个 HTML 字符串(可能包含无效的 HTML,如缺少标签,这不是我的错)转换为 DOM 对象。在这
我正在抓取以下 html 表格: A 5 6 我的目标是使用 Html_simple_dom 来解析数据并将值输入到 MySQL 数据库中。这是我到目前为止所拥有的: find
这个问题在这里已经有了答案: Scrape web page data generated by javascript (2 个答案) 关闭 8 年前。 我正在使用“simple_html_dom”
以下是我的代码: $html = $_GET['html']; $dom = file_get_html($html); $h1 = $dom->find('h1'); echo $h1[0]; 它返
Country De
我正在尝试将这些语句的结果插入 MySQL 数据库。数据库设置正确。当我插入数据库时,我得到 3 个点“...”,而不是我应该得到的。最后一个电话正确存储,只有前两个不存储到数据库中,只存储点。
我想做的是将不为空的值存储到数据库中,我的数据库连接是正确的,这里的问题是当我存储到数据库中时,只有 Id 递增,这是因为 Id 设置为自动递增。 这是我的代码: include("connect.p
我想使用 simple_html_dom.php 从 HTML 文档中删除空段落.我知道如何使用 DOMDocument 类来做到这一点,但是,因为我使用的 HTML 文件是在 MS Word 中准备
我有一个字符串变量 $str其中包含页面的一部分的 HTML。我需要添加 CSS 属性 text-align:justify所有 HTML 中的元素(最好使用 simple_html_dom 解析器
我正在使用 simple_html_dom 开发网络抓取应用程序.我需要提取网页中的所有图像。以下是可能性: 标记图片 如果有 的 css在同一页面中标记。 如果有内嵌样式的图片,带或使用其他标签。
是否可以在 php 中使用 simple_html_dom 从 dom 获取所有具有 href 作为属性的元素 并获取该 href 的值? 最佳答案 是的,你会这样做: foreach($html->
我是一名优秀的程序员,十分优秀!