【问题标题】:How to extract meta tags from website?如何从网站中提取元标记?
【发布时间】:2013-02-19 05:34:40
【问题描述】:

我目前正在使用 get_meta_tags 从各种网站获取标签,如下所示:

<?php $tags = get_meta_tags('http://www.stackoverflow.com/'); ?>

这是我用来显示该信息的代码:

<?php echo $tags['description']; ?><br /><br />
<?php echo $tags['keywords']; ?>

现在有两件事我不知道该怎么做:

  1. 如果元描述不存在,如何删除&lt;br /&gt;&lt;br /&gt;?基本上做到这一点,顶部没有多余的线条导致空位。

  2. 如何使关键字成为指向我的域的所有链接,例如 http://mysite.com/keyword/coding``http://mysite.com/keyword/website-builderhttp://mysite.com/keyword/php-help?

【问题讨论】:

  • 第一个:echo $tags['description'] ? $tags['description']."

    " : '';
  • @Akam 提示:在 cmets 中使用反引号分隔代码
  • @Akam 这对我不起作用,description 我相信两次错误地出现在其中。

标签: php meta


【解决方案1】:

这应该可以解决问题:

<?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 语句的短符号,我个人更喜欢长符号。

【讨论】:

  • 有什么办法可以把它从+号改成-号?
  • 你可以使用 str_replace 来做到这一点 -> php.net/manual/en/function.str-replace.php
  • 我刚刚完成了测试,它显示了关键字列表和 URL 列表。但是,我真正想要的是类似于&lt;a href="http://www.mysite.com/keywords/first-keyord"&gt;first keyword&lt;/a&gt;, &lt;a href="http://www.mysite.com/keywords/second-keyord"&gt;second keyword&lt;/a&gt;, &lt;a href="http://www.mysite.com/keywords/third-keyord"&gt;third keyword&lt;/a&gt;, 等的东西。抱歉,如果我在上面没有正确解释。
  • 我很乐意提供帮助,但您至少应该尝试自己动手。 1.) 删除 echo $tags['keywords'] -> 行,以便不再打印关键字。 2.) 更改其中包含 url 的行,使其符合您的需要,可能类似于:echo '&lt;a href="http://www.mysite.com/keywords/"'.trim($keyword).'"&gt;'.trim($keyword).'&lt;/a&gt;';
  • 在你真正提到之前我已经完成了第一部分,这是我一直坚持的第二部分。这是我输入的内容:echo '&lt;a href="http://www.mysite.com/keywords/"'.trim($keyword).'"&gt;'.trim($keyword).'&lt;/a&gt;, ';,但是当它显示在页面上时,它是关键字列表,但它们都直接链接到http://www.mysite.com/keywords/,所以我不确定为什么第二个'.trim($keyword).' 不起作用。 ..
猜你喜欢
  • 1970-01-01
  • 2013-04-04
  • 2015-04-21
  • 2020-08-25
  • 2014-04-17
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
相关资源
最近更新 更多