Skip to content

Commit

Permalink
Reto #18 - php
Browse files Browse the repository at this point in the history
  • Loading branch information
almonteagudor committed Aug 11, 2023
1 parent 99b26a9 commit 8e0a7c0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Retos/Reto #18 - WEB SCRAPING [Difícil]/php/almonteagudor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

$url = "https://holamundo.day/";
$text = "Agenda 8 de mayo";

$document = new DOMDocument();

$document->loadHTMLFile($url);

$elements = $document->getElementsByTagName("h1");

$element = searchElementByString($elements, $text);

if($element) {
echo $element->textContent . "\n";

while($element->nextElementSibling?->tagName === 'blockquote') {
$element = $element->nextElementSibling;

echo $element->textContent . "\n";
}
} else {
echo "No se ha encontrado: $text\n";
}

function searchElementByString(DOMNodeList $list, string $text): ?DOMElement
{
foreach ($list as $element) {
if(str_contains($element->textContent, $text)) {
return $element;
}
}

return null;
}

0 comments on commit 8e0a7c0

Please sign in to comment.