繁体   English   中英

从多个URL提取标题和描述

[英]Extract Title and Description from multiple URLs

我有所有文章的链接列表。 我正在尝试使用PHP一次提取所有标题和描述。 我还希望文章标题超链接到URL,并在其下方以斜体显示说明。

我的问题是这样的:当我为一个链接执行此操作时,它可以工作,但是当我尝试多个链接时,或者即使我重复代码并手动粘贴到每个链接中,也无法正常工作。 以下是我的适用于一个链接的代码。 有任何想法吗?

    <html>
    <a href="http://bit.ly/18EFx87">
    <b><?php

    function getTitle($Url){
        $str = file_get_contents($Url);
        if(strlen($str)>0){
            preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
            return $title[1];
        }
    }
    echo getTitle("http://bit.ly/18EFx87");

    ?></b><br>
    </a>
    <i><?php
    $tags = get_meta_tags('http://bit.ly/18EFx87');
    echo $tags['description'];
    ?></i>
    </html>

我假设您的意思是多个URL,那么类似的方法将起作用。

<html>

<?php
function getTitle($url){
    @$str = file_get_contents($url); // suppressing the warning
    if(strlen($str)>0){
        preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
        return $title[1];
    } else {
        return false;
    }
}

$urls = array('http://bit.ly/18EFx87', 'url2');
foreach($urls as $url)
{
    $title = getTitle($url);
    if($title === false)
    {
        continue;
    }
    echo '<a href="' . $url . '"><b>';
    echo $title;

    echo '</b></a><br><i>';

    $tags = get_meta_tags($url);
    echo $tags['description'] . '</i>';
 } 
 ?>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM