Page 1 of 1
Is there a way to get the Title Tag through PHP?
Posted: Tue Oct 21, 2008 3:45 pm
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?
Re: Is there a way to get the Title Tag through PHP?
Posted: Tue Oct 21, 2008 4:11 pm
by dude81
I believe you can use fopen and parse using regular expression for title tag
Re: Is there a way to get the Title Tag through PHP?
Posted: Tue Oct 21, 2008 4:21 pm
by requinix
file_get_contents and
Code: Select all
preg_match("#<title>(.*?)</title>#i", $html, $matches);
echo htmlentities($matches[1]);
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 9:53 am
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?
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 9:56 am
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?
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 10:01 am
by VladSun
You mean that the title tag is set by a JavaScript function?
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 11:14 am
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.
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 11:18 am
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.
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 11:21 am
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?!?
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 11:27 am
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.
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 11:32 am
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.
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 11:38 am
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..
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 12:46 pm
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?
Re: Is there a way to get the Title Tag through PHP?
Posted: Wed Oct 22, 2008 1:50 pm
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.