1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| $url= "https://en.wikipedia.org/wiki/Raphinha_(footballer,_born_1996)";
libxml_use_internal_errors(true);
$parser = new DOMDocument();
$parser->loadHTMLFile($url);
//Extractions the title using #firstHeading
$wikiped_title = $parser->getElementById("firstHeading");
$wikipedia_title = $wikiped_title->nodeValue; // modifié
echo $wikipedia_title."<br><br>";
$paragraphs = $parser->getElementsByTagName("p"); // modifié
$description = "";
foreach($paragraphs as $paragraph) {
// "str_starts_with" et "str_contains" ::: PHP8
// if(str_starts_with($paragraph, $wikipedia_title) or str_contains($paragraph, $wikipedia_title)) {
// echo $paragraph->nodeValue."<br><br>";
if (stripos($paragraph->nodeValue, $wikipedia_title) !== false) {
// On récupère la première valeur (texte) du tout 1er élément de tous les paragraphes qui commence par le Titre Wiki:
$description = $paragraph->nodeValue->item(0);
}
}
echo $description."<br><br>"; |
Partager