Jump to content

Getting the Title of a URL

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Hey guys,

I've built a web directory just for pure practice, and for some strange reason the code I've built to get the webpage title only works on some websites, here is the code I'm using:

function getTitle($link){
    $str = file_get_contents($link);    if(strlen($str)>0){        preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);        return $title[1];    }}

It works for some webpages, other it doesn't, when I try codecall.net it gives the error 301 Moved Permanently...

Try it yourself: The Hyperlink Directory - The ultimate web directory and backlink creator

Thanks guys, all help is appreciated.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
It depends on the fact that many web sites automatically redirects to another URL, like http://www.codecall.net becomes http://forum.codecall.net/forum.php. What you can do is to use for example cURL instead of file_get_contents() and there check the return code (301, 200, 404 etc) and act on the code.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users