简体   繁体   English

如何获得URL的标题?

[英]How to get URL's Title?

if ($link == true) {
    // Links
    $link_search = '/\[a\](.*?)\[\/a\]/i';

    if (preg_match_all($link_search, $text, $matches)) {

        foreach ($matches[1] as $match) {
            $match_decode = urldecode($match);
            $match_url = $match_decode;

            if (!preg_match("/http\:\/\//", $match_decode)) {
                $match_url = 'http://' . $match_url;
            }

            $text = str_replace('[a]' . $match . '[/a]', '<a href="' . strip_tags($match_url) . '" target="_blank" rel="nofollow">' . $match_decode . '</a>', $text);
        }
    }
}

The above code displays the link posted and when clicked redirects to the link. 上面的代码显示发布的链接,单击后将重定向到该链接。 What I want is to show the of the destination link. 我要显示的是目标链接。 For ex. 对于前。 Stackoverflow is shown in the post and when clicked takes user to http://stackoverflow.com Stackoverflow显示在帖子中,单击后将用户带到http://stackoverflow.com

Try replacing: 尝试更换:

$text = str_replace('[a]' . $match . '[/a]', '<a href="' . strip_tags($match_url) . '" target="_blank" rel="nofollow">' . $match_decode . '</a>', $text);

with: 有:

$str = file_get_contents($match_url);
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
$text = str_replace('[a]' . $match . '[/a]', '<a href="' . strip_tags($match_url) . '" target="_blank" rel="nofollow">' . $title[1] . '</a>', $text);

to get images: 获取图像:

preg_match("/\<img\>(.*)\<\/img\>/",$str,$img);

You will get an array in $img, you will have to loop through it: 您将在$ img中获得一个数组,您将不得不遍历它:

for($i=0; $i<count($img); $i++){
    echo $img[$i];
}

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

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