Skip to content

Commit

Permalink
Merge pull request #4602 from almonteagudor/Reto#18-php
Browse files Browse the repository at this point in the history
Reto #18 - php
  • Loading branch information
Roswell468 authored Aug 12, 2023
2 parents 3b674de + 8e0a7c0 commit b42c723
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 b42c723

Please sign in to comment.