如何从网站提取元标记? [英] How to extract meta tags from website?
问题描述
我目前正在使用get_meta_tags从各种网站获取标签,如下所示:
I'm currently using get_meta_tags to get the tags from a variety of websites like this:
<?php $tags = get_meta_tags('http://www.stackoverflow.com/'); ?>
这是我用来显示该信息的代码:
This is the code I'm using to display that information:
<?php echo $tags['description']; ?><br /><br />
<?php echo $tags['keywords']; ?>
现在有两件事我不知道该怎么做:
Now there are two things I can't figure out how to do:
-
如果元描述不存在,如何删除
<br /><br />
?基本上这样做是在顶部没有多余的行导致空白.
How do I make it so that
<br /><br />
is removed if the meta description doesn't exist? Basically do that there isn't extra lines at the top causing an empty spot.
如何使关键字全部链接到我的域,例如
http://mysite.com/keyword/coding``http://mysite.com/keyword/website-builder
或http://mysite.com/keyword/php-help
?
How can I make the keywords all links to my domain such as
http://mysite.com/keyword/coding``http://mysite.com/keyword/website-builder
or http://mysite.com/keyword/php-help
?
推荐答案
这应该可以解决问题:
<?php
$tags = get_meta_tags('http://www.ebay.com/');
if(trim($tags['description'])!='') //if description is set and not empty
{
echo ($tags['description']).'<br /><br />';
}
echo $tags['keywords'];
$keywordArray = explode(",", $tags['keywords']); //split string with keywords in an array
foreach($keywordArray as $keyword) //for each entry in the array
{
echo "http://www.mysite.com/".urlencode(trim($keyword)); //echo your URL. Encode the keyword in case special chars are present
}
?>
Akam给您if语句的缩写,我个人更喜欢long表示.
Akam gave you the short notation of the if-statement, I personally prefer the long notation.
这篇关于如何从网站提取元标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!