开发者

Best way to get the contents of TITLE TAG from page URL

开发者 https://www.devze.com 2023-01-08 11:29 出处:网络
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

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 开发者_Python百科like the link to say the title of the referring page.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消