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
| <?php
include("./include/identifiants.php");//fichier définissant la fonction connexion_bdd()
function getMetaTitle($content){
$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
if(preg_match($pattern, $content, $match))
return $match[1];
else
return false;
}
function getMetaDescription($content) {
$metaDescription = false;
$metaDescriptionPatterns = array("/]*>/Ui", "/]*>/Ui");
foreach ($metaDescriptionPatterns as $pattern) {
if (preg_match($pattern, $content, $match))
$metaDescription = $match[1];
break;
}
return $metaDescription;
}
function getExcerpt($content) {
$text = html_entity_decode($content);
$excerpt = array();
//match all tags
preg_match_all("|<[^>]+>(.*)]+>|", $text, $p, PREG_PATTERN_ORDER);
for ($x = 0; $x < sizeof($p[0]); $x++) {
if (preg_match('< p >i', $p[0][$x])) {
$strip = strip_tags($p[0][$x]);
if (preg_match("/\./", $strip))
$excerpt[] = $strip;
}
if (isset($excerpt[0])){
preg_match("/([^.]+.)/", $strip,$matches);
return $matches[1];
}
}
return false;
}
connexion_bdd(); // connexion à la base de données
if(isset($_POST['lien']))
{
$url = $_POST['lien'];
if ($content = file_get_contents($url))
{
$content=mysql_real_escape_string($content);
$title = mysql_real_escape_string(getMetaTitle($content));
$description = mysql_real_escape_string(getMetaDescription($content));
$excerpt = getExcerpt($content);
$lien=mysql_real_escape_string($_POST['lien']);
mysql_query("INSERT INTO articles(id, liens, titre, description, temps) VALUES('', '".$lien."', '".$title."', '".(($description!='')?$description:$excerpt)."', ".time().")") or die (mysql_error()); // lien, temps, titre, description
$insert='ok';
}
}
else
{
$insert='ko';
}
header('Location:index.php?insert='.$insert);
?> |
Partager