tags:

views:

51

answers:

2

Given a URL. What is the best way to get the contents of the title tag in the URL.

Basically I want to check for the http_referrer and if it exists give a link back to the referring page. But I would like the link to say the title of the referring page.

+2  A: 

The <title> tag of page X's referring page is not going to be stored anywhere in X. You will need to request the referring page to get its <title> tag.

Here is a link to some PHP code that will do this: Grab the title of a web page (local or remote)

I'm going to alter the code a bit to fit your use-case:

<?php
    $file = @ fopen($_SERVER['HTTP_REFERER'],"r") or die ("Can't open HTTP_REFERER.");
    $text = fread($file,16384);
    if (preg_match('/<title>(.*?)<\/title>/is',$text,$found)) {
        $title = $found[1];
    } else {
        $title = " -- no title found -- ";
    }
?>

Just keep in mind that you can't trust the HTTP_REFERER variable, since the browser (or plugins, etc.) can change it. (1)

GoalBased
A: 

As GoalBased suggested, first load the document at the given URL. Then:

1) Use any of the multitude of PHP HTML parsers to find the title tag.

2) Or, if you want it quick and dirty, use a regular expression to find the string <title>(.*)</title>, which is probably much faster than parsing, but might give you a false positive once in a while.

JeSuisse