Filtering Date In PHP DOM

余生长醉 提交于 2019-12-10 22:12:28

问题


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;

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


回答1:


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;
?>


来源:https://stackoverflow.com/questions/13361347/filtering-date-in-php-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!