-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4602 from almonteagudor/Reto#18-php
Reto #18 - php
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Retos/Reto #18 - WEB SCRAPING [Difícil]/php/almonteagudor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |