PHPNewbie55 Posted April 21, 2008 Share Posted April 21, 2008 I am trying to grab the title of an URL Here is what I am trying... But I can't grab the title.. just what is inbetween the <body> and </body> tags.... This code will grab what ever is inbetween the <span> and </span> tags.... <?php $url = "http://www.anyurl-anywhere-online.com"; $data = file_get_contents("".$url.""); preg_match('/<span>.*?<\/span>/',$data, $matches); $title = "".$matches['0'].""; print "".$title.""; ?> So why won't this grab the title..??? <?php $url = "http://www.anyurl-anywhere-online.com"; $data = file_get_contents("".$url.""); preg_match('/<title>.*?<\/title>/',$data, $matches); $title = "".$matches['0'].""; print "".$title.""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/102081-solved-fetching-the-title-of-an-url/ Share on other sites More sharing options...
GingerRobot Posted April 21, 2008 Share Posted April 21, 2008 The problem is that it is working - it's just also matching the <title> tags. If you were to view the source of the above, you would find the title tags. Nothing is displayed because title tags don't display anything. To grab just the title and not the tags too, use: <?php $url = "http://www.anyurl-anywhere-online.com"; $data = file_get_contents($url); preg_match('/<title>(.*?)<\/title>/',$data, $matches); $title = $matches[1]; print $title; ?> By the way, there's no need to put empty strings before all your variables. Quote Link to comment https://forums.phpfreaks.com/topic/102081-solved-fetching-the-title-of-an-url/#findComment-522620 Share on other sites More sharing options...
PHPNewbie55 Posted April 21, 2008 Author Share Posted April 21, 2008 Thanks... That didn't actually work.. but if I use this.. <?php print "".str_replace("<title>", "", str_replace("</title>", "", $title)).""; ?> It will display... Quote Link to comment https://forums.phpfreaks.com/topic/102081-solved-fetching-the-title-of-an-url/#findComment-522622 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.