在PHP DOM中过滤日期 [英] Filtering Date In PHP DOM
问题描述
我想使用SIMPLE HTML PHP DOM PARSER(simplehtmldom.sourceforge.net)从获取的内容中使用空格替换所有日期。以下是代码:
I wanna replace all date with a space from the fetched content using SIMPLE HTML PHP DOM PARSER (simplehtmldom.sourceforge.net). Here is the code:
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
或 2004年1月1日
或 Dec 12 14
应替换为空格...但不替换那些日期与空间..现在该怎么办?
这是一个例子,显示它将如何工作。 http://codepad.org/lAuHW565 但是为什么它不工作在 PHP简单HTML DOM解析器
So, here all date data like: 01 Jan 2004
or Jan 01 2004
or Dec 12 14
should be replaced by a space... But its not replacing those date with space.. Now what to do?
Here is a example showing how it will work.. http://codepad.org/lAuHW565 but why its not working in PHP Simple HTML DOM Parser
推荐答案
您正试图用 SimpleHTML
对象是不可能的(它是一个对象,而不是一个字符串)。您应该做的是首先获取HTML,然后替换,然后使用 str_get_html
函数将其变为 SimpleHTML
。
You're trying to replace on a SimpleHTML
object which is impossible (it's an object, not a string). What you should do is first get the HTML, then replace, and then turn it into SimpleHTML
using the str_get_html
function.
<?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 DOM中过滤日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!