获取指定文本PHP之间的字符串

时间:2017-05-22 09:07:33

标签: php dom

我试图从中提取日期和时间,我该怎么做呢?

我的代码

    <?php
$url = file_get_contents('http://webcache.googleusercontent.com/search?q=cache:http://unitedseo.ae');
    $doc = new DOMDocument();
    $doc->loadHTML($url);
    $node = $doc->getElementById('google-cache-hdr');
    echo $doc->saveHtml($node), PHP_EOL;
?>

我的代码返回此

<body><div id="google-cache-hdr" dir="ltr">
<div>This is Google's cache of <a href="http://www.unitedseo.ae/" dir="ltr">http://www.unitedseo.ae/</a>. It is a snapshot of the page as it appeared on May 20, 2017 05:16:23 GMT. </div>
<div>The <a href="http://www.unitedseo.ae/" dir="ltr">current page</a> could have changed in the meantime. <a href="http://support.google.com/websearch/bin/answer.py?hl=en&amp;p=cached&amp;answer=1687222">Learn more</a>
</div>
<div></div>
<div>
<span style="display:inline-block !important;margin-top:8px !important;margin-right:104px !important;white-space:nowrap !important;"><span style="margin-right:28px !important;"><span style="font-weight:bold !important;">Full version</span></span><span style="margin-right:28px !important;"><a href="http://webcache.googleusercontent.com/search?q=cache:http://unitedseo.ae&amp;num=1&amp;ie=UTF-8&amp;prmd=ivns&amp;strip=1&amp;vwsrc=0">Text-only version</a></span><span style="margin-right:28px !important;"><a href="http://webcache.googleusercontent.com/search?q=cache:http://unitedseo.ae&amp;num=1&amp;ie=UTF-8&amp;prmd=ivns&amp;strip=0&amp;vwsrc=1">View source</a></span></span><span style="display:inline-block !important;margin-top:8px !important;color:#717171 !important;">Tip: To quickly find your search term on this page, press <b>Ctrl+F</b> or <b>⌘-F</b> (Mac) and use the find bar.</span>
</div>
</div>

</body>

3 个答案:

答案 0 :(得分:0)

希望这会有所帮助。

Context WebRoles
[-] Has installed proper web roles 2.39s
  Expected string length 27 but was 28. Strings differ at index 20.
  Expected: {IIS-DefaultDocument_Enabled}
  But was:  {IIS-DefaultDocument_Disabled}
  -------------------------------^
  284:                 $toCompare | Should Be ("{0}_{1}" -f $_,"Enabled")

<强>输出:

<?php

ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$url = file_get_contents('http://webcache.googleusercontent.com/search?q=cache:http://unitedseo.ae');
$doc = new DOMDocument();
$doc->loadHTML($url);
$node = $doc->getElementById('google-cache-hdr');
$results = $doc->saveHtml($node);
preg_match("#\d{1,2}\s[a-zA-Z]{3}\s\d{4}\s\d{2}:\d{2}:\d{2}#", $results, $matches);
print_r($matches);

答案 1 :(得分:0)

正则表达式可能是最好的解决方案,但我也可以试用strpos方法。

如果您要搜索文字appeared on,请获取该位置,然后搜索.</div>的位置,您可以找到您的时间位置并将其解压缩。

答案 2 :(得分:0)

未经测试

<?php
    $url = file_get_contents('http://webcache.googleusercontent.com/search?q=cache:http://unitedseo.ae');
    $doc = new DOMDocument();
    $doc->loadHTML($url);
    $node = $doc->getElementById('google-cache-hdr');
    echo $rs = $doc->saveHtml($node);
    preg_match('/(\d?\d [A-Za-z]+ \d\d\d\d) (\d\d\:\d\d)/', $rs, $matches);
    echo $matches[0];
    print_r($matches);die;
    die;
?>