A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearFastest way to retrieve a <title> in PHP - page 1
User InfoPosts
Fastest way to retrieve a <title> in PHP#1
I'm doing a bookmarking system and looking for the fastest (easiest) way to retrieve a page's title with PHP.

It would be nice to have something like $title = page_title($url)

posted date: 2008-12-29 18:01:00

Re: Fastest way to retrieve a <title> in PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-29 18:01:01

Re: Fastest way to retrieve a <title> in PHP#3
Regex? Use cURL to get the $htmlSource variable's contents. preg_match('/<title>(.*)<\/title>/iU', $htmlSource, $titleMatches);print_r($titleMatches);see what you have in that array.Most people say for HTML traversing though you should use a parser as regexs can be unreliable.The other answers provide more detail :)

posted date: 2008-12-29 18:07:00

Re: Fastest way to retrieve a <title> in PHP#4
perhaps it should be changed to non greedy to make it safer

posted date: 2008-12-29 18:11:00

Re: Fastest way to retrieve a <title> in PHP#5
You can grab $htmlSource with curl or fopen.

posted date: 2008-12-29 18:13:00

Re: Fastest way to retrieve a <title> in PHP#6
<?php function page_title($url) { $fp = file_get_contents($url); if (!$fp) return null; $res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches); if (!$res) return null; // Clean up title: remove EOL's and excessive whitespace. $title = preg_replace('/\s+/', ' ', $title_matches[1]); $title = trim($title); return $title; }?>Gave 'er a whirl on the following input:print page_title("http://www.google.com/");Outputted: GoogleHopefully general enough for your usage. If you need something more powerful, it might not hurt to invest a bit of time into researching HTML parsers.EDIT: Added a bit of error checking. Kind of rushed the first version out, sorry.

posted date: 2008-12-29 18:15:00

Re: Fastest way to retrieve a <title> in PHP#7
or making this simple function slightly more bullet proof:function page_title($url) { $page = @file_get_contents($url); if (!$page) return null; $matches = array(); if (preg_match('/<title>(.*?)<\/title>/', $page, $matches)) { return $matches[1]; } else { return null; }}echo page_title('http://google.com');

posted date: 2008-12-29 18:23:00

Re: Fastest way to retrieve a <title> in PHP#8
Yeah, I got caught once by a page with two title tags. Add the question mark after the asterisk.

posted date: 2008-12-30 04:06:00

Re: Fastest way to retrieve a <title> in PHP#9
I like using SimpleXml with regex's, this is from a solution I use to grab multiple link headers from a page in an OpenID library I've created. I've adapted it to work with the title (even though there is usually only one).function getTitle($sFile){ $sData = file_get_contents($sFile); if(preg_match('/<head.[^>]*>.*<\/head>/is', $sData, $aHead)) { $sDataHtml = preg_replace('/<(.[^>]*)>/i', strtolower('<$1>'), $aHead[0]); $xTitle = simplexml_import_dom(DomDocument::LoadHtml($sDataHtml)); return (string)$xTitle->head->title; } return null;}echo getTitle('http://www.momige.com/399332/fastest-way-to-retrieve-a-title-in-php');Ironically this page has a "title tag" in the title tag which is what sometime causes problems with the pure regex solutions. This solution is not perfect as it lowercase's the tags which could cause a problem for the nested tag if formatting/case was important (such as XML), but there are ways that are a bit more involved around that problem.

posted date: 2008-12-31 00:09:00

Re: Fastest way to retrieve a <title> in PHP#10
Great! That's good.

posted date: 2009-01-02 11:46:00

Re: Fastest way to retrieve a <title> in PHP#11
Oh. Too right. If preg_match doesn't get a result, the reference to $title_matches will barf. Will tidy up a bit.

posted date: 2009-01-06 17:12:00

Re: Fastest way to retrieve a <title> in PHP#12
I was looking for a better way to do that, but looks like most people use your proposed solution as a fast method to retrieve the title. Please consider using the 's' modifier, i've seen weird situations where a new line breaks the regex

posted date: 2009-11-17 03:55:00

select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.