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)
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)
A function to handle title tags that have attributes added to them
You can get it without reg expressions:
or making this simple function slightly more bullet proof:
Regex?
Use cURL to get the $htmlSource variable's contents.
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 :)
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).
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.
Gave 'er a whirl on the following input:
Outputted: Google
Hopefully 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.