2012-06-15 2 views
0

나는이 제목을페이지 키워드, 설명 및 제목을 스크랩하는 기능?

function getPageTitle ($url) 
{ 
    $content = $url; 
    if (eregi("<title>(.*)</title>", $content, $array)) { 
     $title = $array[1]; 
     return $title; 
    } 
} 

를 스크랩하는 최초의 기능입니다 제목, 설명 및 간단한 HTML 페이지 의 키워드를 스크랩 간단한 3 개 기능을 썼다는 잘 작동하고 그 설명 및 키워드를 스크랩이 개 기능을하며,

function getPageKeywords($url) 
{ 
    $content = $url; 
    if (preg_match('/<meta[\s]+[^>]*?name[\s]?=[\s\"\']+keywords[\s\"\']+content[\s]?=[\s\"\']+(.*?)[\"\']+.*?>/i', $content, $array)) { 
     $keywords = $array[1]; 
     return $keywords; 
    } 
} 
function getPageDesc($url) 
{ 
    $content = $url; 
    if (preg_match('/<meta[\s]+[^>]*?name[\s]?=[\s\"\']+description[\s\"\']+content[\s]?=[\s\"\']+(.*?)[\"\']+.*?>/i', $content, $array)) { 
     $desc = $array[1]; 
     return $desc; 
    } 
} 

내가 거기는 preg_match 라인에 문제가 있습니다 만, 난 정말 내가 너무 많은 일을하려고 모르지만 그것은

작동하지 않습니다 알고

를 작동하지 않는 사람들3210
+1

참고 : 'eregi'는 더 이상 사용되지 않습니다. http://php.net/manual/en/function.eregi.php – Will

+1

regex를 사용하여 HTML을 파싱하면 단순한 태그 쌍보다 더 복잡한 것이됩니다. 태그 속성을 파싱하기 시작할 때 PHP Dom으로 전환해야합니다. http://php.net/manual/en/book.dom.php 이름, 설명 및 내용 속성이 있어야한다는 것입니다. 일치하는 순서. – Sp4cecat

+0

세 번째 중요한 점은 웹 페이지에 있기 때문에 원하는 권한으로 데이터를 사용할 수 있다는 의미는 아닙니다. (허가없이. –

답변

2

왜 get_meta_tags를 사용하지? PHP Documentation Here

<?php 
// Assuming the above tags are at www.example.com 
$tags = get_meta_tags('http://www.example.com/'); 

// Notice how the keys are all lowercase now, and 
// how . was replaced by _ in the key. 
echo $tags['author'];  // name 
echo $tags['keywords'];  // php documentation 
echo $tags['description']; // a php manual 
echo $tags['geo_position']; // 49.33;-86.59 
?> 

참고 당신은 URL, 로컬 파일 또는 문자열 중 하나에 매개 변수를 변경할 수 있습니다.

+0

Thx 해결 중입니다. – Marco

1

PHP의 네이티브 DOMDocument을 사용하여 HTML을 파싱 한 다음 regex를 사용하는 것이 더 나을 것입니다. 사이트의 연령대에 따라이 키워드를 사용할 수도 있습니다. 설명 태그는 더 이상 존재하지 않으므로 항상 거기에 의존합니다. . 그러나 여기 당신이 DOMDocument를 함께 할 수있는 방법입니다 :

<?php 
$source = file_get_contents('http://php.net'); 

$dom = new DOMDocument("1.0","UTF-8"); 
@$dom->loadHTML($source); 
$dom->preserveWhiteSpace = false; 

//Get Title 
$title = $dom->getElementsByTagName('title')->item(0)->nodeValue; 

$description = ''; 
$keywords = ''; 
foreach($dom->getElementsByTagName('meta') as $metas) { 
    if($metas->getAttribute('name') =='description'){ $description = $metas->getAttribute('content'); } 
    if($metas->getAttribute('name') =='keywords'){ $keywords = $metas->getAttribute('content'); } 
} 

print_r($title); 
print_r($description); 
print_r($keywords); 
?> 
관련 문제