php - 在 PHP DOM 中过滤日期

标签 php parsing dom simple-html-dom

我想使用 SIMPLE HTML PHP DOM PARSER (simplehtmldom.sourceforge.net) 将获取的内容中的所有日期替换为空格。代码如下:

include("simple_html_php_dom.php");
$html = file_get_html("http://freebacklinks.prijm.com"); //example.com
$result = "$html";
$result = preg_replace("/([1-9]|[0-2][0-9]|3[0-1]) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{4}/", " ", $result);
$result = preg_replace("/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([1-9]|[0-2][0-9]|3[0-1]) [0-9]{4}/", " ", $result);
echo $result;

因此,此处所有日期数据,例如:01 Jan 2004Jan 01 2004Dec 12 14 应替换为空格。 ..但它不会用空格替换那些日期..现在该怎么办?
这是一个展示其工作原理的示例。 http://codepad.org/lAuHW565 但为什么它在 PHP Simple HTML DOM Parser 中不起作用?

最佳答案

您正在尝试替换 SimpleHTML 对象,这是不可能的(它是一个对象,而不是字符串)。您应该做的是首先获取 HTML,然后替换,然后使用 str_get_html 函数将其转换为 SimpleHTML

<?php
    include("simple_html_php_dom.php");

    //Start with getting the pure HTML and replacing in that (don't use SimpleHTMLPHP for this)
    $html = file_get_contents("http://freebacklinks.prijm.com"); //example.com
    $html= preg_replace("/([1-9]|[0-2][0-9]|3[0-1])\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+[0-9]{4}/", " ", $html);
    $html = preg_replace("/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+([1-9]|[0-2][0-9]|3[0-1])\s+[0-9]{4}/", " ", $html);

    //Now create the $result variable:
    $result = str_get_html($html);
    echo $result;
?>

关于php - 在 PHP DOM 中过滤日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13361347/

相关文章:

macos - Nginx + PHP-FPM 在 Mountain Lion 上非常慢

java - 寻找一种解析多类型 JSON 字段的方法

java - NO_MODIFICATION_ALLOWED_ERR : An attempt is made to modify an object where modifications are not allowed

javascript - 从 javascript 外部文件无法从 HTML 文件获取 id 并替换内容

node.js - 为什么 bodyparser.json 正在解析 multipart/formdata?

java - 如何在 GWT 中创建 documentFragment?

php - 使用 PHP/CSS 显示图像

php - 使用 php 无法进行搜索

php - 这个查询出了什么问题?

Python 和解析 unicode 文件