Is there a way to get the Title Tag through PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acimag
Forum Newbie
Posts: 2
Joined: Tue Oct 21, 2008 3:41 pm

Is there a way to get the Title Tag through PHP?

Post by acimag »

What im trying to do is have a page that has a link

Basically the link would have the URL and the Page Title
(almost like a send to a friend)

I just need to access the Title Tag of that specific page.

I know

$URL = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
(gets the page url)

Now how would I get the

$TITLE tag? I know this is a 123 answer. Any Takers?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Is there a way to get the Title Tag through PHP?

Post by dude81 »

I believe you can use fopen and parse using regular expression for title tag
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is there a way to get the Title Tag through PHP?

Post by requinix »

file_get_contents and

Code: Select all

preg_match("#<title>(.*?)</title>#i", $html, $matches);
echo htmlentities($matches[1]);
acimag
Forum Newbie
Posts: 2
Joined: Tue Oct 21, 2008 3:41 pm

Re: Is there a way to get the Title Tag through PHP?

Post by acimag »

in researching I have found that

preg_match("#<title>(.*?)</title>#i", $html, $matches);
echo htmlentities($matches[1]);


Will only work with preg_match if the title is hard coded. We are dynamically creating titles
and my other programing friend said this will now work.

I asked if I can use javascript and get element by id. But he said that Javascript does not work with PHP that way.

But I know there has to be a solution. Any Ideas?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Is there a way to get the Title Tag through PHP?

Post by onion2k »

acimag wrote:Will only work with preg_match if the title is hard coded. We are dynamically creating titles
and my other programing friend said this will now work.
Why won't it work? What reason did your "programing friend" give?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Is there a way to get the Title Tag through PHP?

Post by VladSun »

You mean that the title tag is set by a JavaScript function?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Is there a way to get the Title Tag through PHP?

Post by dude81 »

unless you are using state change functions on PHP side i.e sessions or cookies, there is every way the above solution should work.
inet411
Forum Newbie
Posts: 11
Joined: Mon Oct 20, 2008 11:14 am

Re: Is there a way to get the Title Tag through PHP?

Post by inet411 »

tasairis wrote:file_get_contents and

Code: Select all

preg_match("#<title>(.*?)</title>#i", $html, $matches);
echo htmlentities($matches[1]);
VladSun wrote:You mean that the title tag is set by a JavaScript function?
tasairis's code will work fine. Even if the title tag is set dynamically using php, using javascript or hard coded in the html - once it hits the user its in the html source. so doing a $html = file_get_contents($url_here); and then tasairis's snippet will get the title.

Try it out. Before saying 'someone' says it won't work. You'll never know unless you try.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Is there a way to get the Title Tag through PHP?

Post by VladSun »

inet411 wrote:
tasairis wrote:Even if the title tag is set dynamically using php, using javascript
.......
Before saying 'someone' says it won't work. You'll never know unless you try.
You mean, that the browser rendered output is available to the PHP script?!?
There are 10 types of people in this world, those who understand binary and those who don't
inet411
Forum Newbie
Posts: 11
Joined: Mon Oct 20, 2008 11:14 am

Re: Is there a way to get the Title Tag through PHP?

Post by inet411 »

VladSun wrote:
inet411 wrote:
tasairis wrote:Even if the title tag is set dynamically using php, using javascript
.......
Before saying 'someone' says it won't work. You'll never know unless you try.
You mean, that the browser rendered output is available to the PHP script?!?
yeah sorta - php gets the same html markup if you do
echo file_get_contents($url);
you will see the same html source as if you went to that website and did a right click view source.

You will see <title>whatever</title>
And that is what acimag is trying to get.

Edit I see what your saying, no of course not 'not the browser rendered output' you get the html code in a string which you can then parse out the title tag.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Is there a way to get the Title Tag through PHP?

Post by onion2k »

You're not getting it. Vlad is saying that the title can be changed with JS when the page loads. If you fetch the page with PHP you'll see the source in the state it was when the page loaded... not how it appears after the JS runs and changes the title.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Is there a way to get the Title Tag through PHP?

Post by dude81 »

inet411 wrote: tasairis's code will work fine. Even if the title tag is set dynamically using php, using javascript or hard coded in the html - once it hits the user its in the html source. so doing a $html = file_get_contents($url_here); and then tasairis's snippet will get the title.

Try it out. Before saying 'someone' says it won't work. You'll never know unless you try.
But what if the URL

Code: Select all

http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]
is a page which needs certain authentication with sessions or cookies or some other methods..
inet411
Forum Newbie
Posts: 11
Joined: Mon Oct 20, 2008 11:14 am

Re: Is there a way to get the Title Tag through PHP?

Post by inet411 »

onion2k wrote:You're not getting it. Vlad is saying that the title can be changed with JS when the page loads. If you fetch the page with PHP you'll see the source in the state it was when the page loaded... not how it appears after the JS runs and changes the title.
I see. Well that is topic worth discussion. Slightly off topic, but why would you change the title tag after the page is fully loaded? So you are saying after the page is loaded and then (1-XX seconds later the title changes?) I'm not trying to be argumentative, just looking for more info here. So there is a title - 'the title' then it changes to - 'another title'? That seems silly. If the title is changed/created onload then it shows in the source. But if its an onclick=changeTitle or something then no there is no way to grab that new title. Could google even get it?


acimag <- do you have a url you can post?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Is there a way to get the Title Tag through PHP?

Post by onion2k »

inet411 wrote:I see. Well that is topic worth discussion. Slightly off topic, but why would you change the title tag after the page is fully loaded? So you are saying after the page is loaded and then (1-XX seconds later the title changes?) I'm not trying to be argumentative, just looking for more info here. So there is a title - 'the title' then it changes to - 'another title'? That seems silly. If the title is changed/created onload then it shows in the source. But if its an onclick=changeTitle or something then no there is no way to grab that new title. Could google even get it?
Rather than "the title" changing to "anther title", it's more often a blank title being populated with a title loaded, for example, from an AJAX application. The title the user sees might not even be in the source of the page that was loaded.

And no, Google couldn't get it. Unless their spider parses and runs Javascript.
Post Reply