Get the title of the url

Discussion in 'PHP' started by coderzone, Aug 12, 2006.

  1. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Experts,

    I am looking for a code snippet that can get me the title of the url I enter.

    something like if I pass
    http://www.go4expert.com/showthread.php?t=2

    It should return Go4Expert - Begining with PHP

    I have checked the internet and seen that curl of php can be useful but could not use it in a very good sense as I am not a PHP expert.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    We can use Regular Expressions to get the title of a page, see the PHP code snippet below which extracts the title of page who's URL you specify.

    PHP:
    $url "http://www.go4expert.com/showthread.php?t=2";
    $file file($url);
    $file implode("",$file);

    if(
    preg_match("/<title>(.+)<\/title>/i",$file,$m))
        print 
    "The title of $url is <b>$m[1]";
    else
        print 
    "The page doesn't have a title tag";
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have added an extra bit of condition
    PHP:
    if (@fclose(@fopen($rightlink"r")))
    so that if the link is not reachable you dont have to open the file.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice