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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
<?php
// +------------------- File_Get ---------------------+
function getMyData($site){
return file_get_contents($site);
}
// +------------------- Scrapper l'url ---------------------+
function spiderView($url,$err_str="Cette url n'est pas valide pour cette fonction !")
{
// Appel à la fonction file_get
$html = getMydata($url);
if($html == ""){
die($err_str); // Si l'url est pas bonne, affiche l'erreur
}
// La source de l'url
$sourceData = $html;
// Pour avoir que du texte
$textData = preg_replace('/(<script.*?>.*?<\/script>|<style.*?>.*?<\/style>|<.*?>|\r|\n|\t)/ms', '', $html);
$textData = preg_replace('/ +/ms', ' ', $textData);
//Selon et /ou avec des majuscules dans les metas
$html = str_ireplace(array("Title","TITLE"),"title",$html);
$html = str_ireplace(array("Description","DESCRIPTION"),"description",$html);
$html = str_ireplace(array("Keywords","KEYWORDS"),"keywords",$html);
$html = str_ireplace(array("Content","CONTENT"),"content",$html);
$html = str_ireplace(array("Meta","META"),"meta",$html);
$html = str_ireplace(array("Name","NAME"),"name",$html);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
//Vérifier les données vides
$site_title = ($title == '' ? "Pas de titre..." : $title);
$site_description = ($description == '' ? "Pas de description..." : $description);
$site_keywords = ($keywords == '' ? "Pas de keywords..." : $keywords);
$arr_meta = array($site_title,$site_description,$site_keywords,$textData);
return $arr_meta;
}
$url2 = "https://www.developpez.com/"; // l'url pour le test
$html2 = spiderView($url2); // appel à la fonction
$texte1 = $html2[3]; //déclarer pour réutiliser par la suite
echo"$texte1";
// + ----------- mes essais.....--------+
//$texte1 = html_entity_decode("$texte1", ENT_QUOTES); //
//if (preg_match("/é|Ã|ô|â|î|è|ê/i", "$texte1"))
// {
// $texte1 = utf8_encode($texte1);
// }
//echo htmlentities($texte1, ENT_QUOTES);
//$texte1 = utf8_encode($texte1);
//$texte1 = utf8_decode($texte1);
//echo mb_detect_encoding($texte1);
?> |
Partager