我想使用 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 2004
或 Jan 01 2004
或 Dec 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/