Is there a way to get the Title Tag through PHP?
Moderator: General Moderators
Is there a way to get the Title Tag through PHP?
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?
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?
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?
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?
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?
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?
Why won't it work? What reason did your "programing friend" give?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.
Re: Is there a way to get the Title Tag through PHP?
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
Re: Is there a way to get the Title Tag through PHP?
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?
tasairis wrote:file_get_contents andCode: Select all
preg_match("#<title>(.*?)</title>#i", $html, $matches); echo htmlentities($matches[1]);
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.VladSun wrote:You mean that the title tag is set by a JavaScript function?
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?
You mean, that the browser rendered output is available to the PHP script?!?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.
There are 10 types of people in this world, those who understand binary and those who don't
Re: Is there a way to get the Title Tag through PHP?
yeah sorta - php gets the same html markup if you doVladSun wrote:You mean, that the browser rendered output is available to the PHP script?!?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.
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?
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?
But what if the URLinet411 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.
Code: Select all
http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]Re: Is there a way to get the Title Tag through PHP?
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?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.
acimag <- do you have a url you can post?
Re: Is there a way to get the Title Tag through PHP?
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.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?
And no, Google couldn't get it. Unless their spider parses and runs Javascript.