"R. Elsenaar" <[EMAIL PROTECTED]> wrote in message
> Hi,
> To get the title of an external page to put into a link, has somebody did
> that already and can give me some hints for this trick
> Robert

// Your question managed to spark my interest and get me to write this...
// WARNING: If you are accepting user input for the page URL, you will
// want to validate it before passing it to this function.
// I use fopen in order to (hopefully) not need to download the entire
// page when we only want something that's likely to be in the first
// few lines.
// This is not guarunteed to get the right title, esp. for dynamic pages
// or anything
// TEST 1: <title>First Test</title>
// TEST 2: <head><title>Second Test</title></head>
// TEST 3: <head><title>Second Test</title></title>

// This function will return the filename or URL of the page if:
// 1. The page has no <title>...</title> tags.
// 2. The page cannot be opened.
// Consider expanding from this concept. It is not extensively tested.

function get_page_title($page) {
    $contents = implode(' ', file($page));
    if(!$contents) return($page); // Something didn't work right.
    if(eregi('<title[^>]*>([^<]*)</title[^>]*>', $contents, $regs)) {
        return(trim(ereg_replace('[[:space:]]+', ' ', $regs[1])));

echo get_page_title($path_to_page);

PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to