Re: Get Page Title

From: Date: Wed, 02 Jan 2002 23:25:07 +0000
Subject: Re: Get Page Title
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-79272@lists.php.net to get a copy of this message
"R. Elsenaar" <info@duikplas.nl> wrote in message news:20020102205936.94192.qmail@pb1.pair.com... > 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]))); } return($page); } echo get_page_title($path_to_page);

« previous php.general (#79272) next »