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>

0

Решение

Надеюсь, это будет полезно.

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

Выход:

Array
(
[0] => 20 May 2017 05:16:23
)
0

Другие решения

Regex, вероятно, будет лучшим решением, но я также мог бы попробовать strpos метод.

Если вы будете искать по тексту appeared on, получить позицию, затем искать позицию .</div> Вы можете узнать, где находится ваше время и извлечь его.

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;
?>
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector