ki Posted October 3, 2007 Share Posted October 3, 2007 How do you get the title of a(n) external webpage? Ive seen it done on the vBulletin's software. Anyone know how to exactly do this? Or is there a PHP function already added to the PHP software that I don't know about. Quote Link to comment https://forums.phpfreaks.com/topic/71658-solved-get-title/ Share on other sites More sharing options...
Norsk.Firefox Posted October 3, 2007 Share Posted October 3, 2007 Probably not the best way: <?php $res_link_1 = 'http://www.phpfreaks.com/'; $file_1 = file_get_contents($res_link_1); preg_match('#<title>(.*?)</title>#s', $file_1, $matches[]); echo '<pre>'; print_r($matches); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71658-solved-get-title/#findComment-360746 Share on other sites More sharing options...
haaglin Posted October 3, 2007 Share Posted October 3, 2007 Taken from PHP.net: <?php $file = fopen ("http://www.example.com/", "r"); if (!$file) { echo "<p>Unable to open remote file.\n"; exit; } while (!feof ($file)) { $line = fgets ($file, 1024); /* This only works if the title and its tags are on one line */ if (eregi ("<title>(.*)</title>", $line, $out)) { $title = $out[1]; break; } } fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71658-solved-get-title/#findComment-360748 Share on other sites More sharing options...
MadTechie Posted October 3, 2007 Share Posted October 3, 2007 parser the index file of the site and read the title in <title>blar</title> something like this <?php $HTML = file_get_contents("http://www.phpfreaks.com"); preg_match_all('%<title>([^<]*)</title>%i', $HTML, $result, PREG_PATTERN_ORDER); $result = $result[0]; echo $result; ?> EDIT: LOL, and the winner is haaglin, for his snip from PHP.NET, which proves better that the other two LOL Quote Link to comment https://forums.phpfreaks.com/topic/71658-solved-get-title/#findComment-360749 Share on other sites More sharing options...
ki Posted October 3, 2007 Author Share Posted October 3, 2007 Thanks guys, they work fine. Does this effect bandwidth at all? And if so, which one if more effective? Quote Link to comment https://forums.phpfreaks.com/topic/71658-solved-get-title/#findComment-360751 Share on other sites More sharing options...
MadTechie Posted October 3, 2007 Share Posted October 3, 2007 the best one for, speed and bandwidth as it, stops when it finds the title.. the other read in the whole page.. Taken from PHP.net: <?php $file = fopen ("http://www.example.com/", "r"); if (!$file) { echo "<p>Unable to open remote file.\n"; exit; } while (!feof ($file)) { $line = fgets ($file, 1024); /* This only works if the title and its tags are on one line */ if (eregi ("<title>(.*)</title>", $line, $out)) { $title = $out[1]; break; } } fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71658-solved-get-title/#findComment-360752 Share on other sites More sharing options...
ki Posted October 3, 2007 Author Share Posted October 3, 2007 Alright, thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/71658-solved-get-title/#findComment-360753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.