简体   繁体   中英

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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