2011-12-08 4 views
0

가능한 중복 :
Fastest way to retrieve a <title> in PHP어떻게 URL의 제목, 이미지, 키워드 및 PHP를 사용하여 설명을 추출하는거야?

웹 사이트 http://www.example.com이 = "예"설명 = 제목으로이 가정은 키워드 = "예, 질문, 사랑의 PHP"은 예입니다 " ".

링크 제출시 가져올 수있는 PHP 코드 또는 다른 코드는 무엇이 될까요?

+0

당신은이 같은 제목을 얻을 수 있습니다 :

가 여기에 컬 PHP 함수 페이지의 http://stackoverflow.com/questions/717100/extract-title-tag-from-html – Alex

답변

4

외부 링크에서 데이터를 가져 오려면 curl을 사용하여 페이지를 가져오고 싶습니다.

Google 
No meta description found 
No meta keywords found 

메타 설명과 키워드가 사용에 대한 더 많은 조정이 필요할 수 있습니다 :

여기이 반환 google.nl를 들어 예를 들어

<?php 
$ch = curl_init("http://www.google.nl"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 
curl_close($ch); 

$title = preg_match('!<title>(.*?)</title>!i', $result, $matches) ? $matches[1] : 'No title found'; 
$description = preg_match('!<meta name="description" content="(.*?)">!i', $result, $matches) ? $matches[1] : 'No meta description found'; 
$keywords = preg_match('!<meta name="keywords" content="(.*?)">!i', $result, $matches) ? $matches[1] : 'No meta keywords found'; 

echo $title . '<br>'; 
echo $description . '<br>'; 
echo $keywords . '<br>'; 
?> 

입니다.

작은 sidenote cURL은 아파치에 기본적으로 설치되지 않으므로 먼저 설치해야 할 수도 있습니다. http://nl3.php.net/manual/en/ref.curl.php

0

질문이 너무 일반적이지만 원하는 도구는 페이지를 가져온 다음 데이터를 찾기 위해 file_get_contents입니다. 이것을 사용하여 제목을 쉽게 찾을 수 있지만 키워드와 설명이 정확히 무엇을 의미하는지는 확실하지 않습니다.

+2

그는 HTML 헤더를 의미 .. 그리고 천국을 위해서 정규 표현식을 쓰지 마라. –