Getting the page title to a PHP variable

I'm not seeing a way to get the page title to a PHP variable.  For example, if the page HTML  has
<title> Table of Contents </title>  I'd like to get "Table of Contents" to a PHP variable.

Thanks for any ideas.

Steve
stevaAsked:
Who is Participating?
 
OmniUnlimitedCommented:
PHP is serverside code that lays out elements in order as it is presented.  There is no code in PHP to check the contents of the elements you lay out.

You can set the title by using the following code:
 
<?php

$title = "Table of Contents";
echo "<title>$title</title>";

?>

Open in new window


But to get the title and put it into a PHP variable, you would need to use something like AJAX.
0
 
Marco GasiFreelancerCommented:
Sorry, but your question is not so clear. Since php is a server side language you should have page title in your php script which send page to the client... Or I'm missing something?
0
 
stevaAuthor Commented:
Yes, you guys are right.   The normal way to do this would be to set the $title variable and them emit it into the HTML.  But still, suppose you had a bunch of pages where someone had already set the title between <title> tags and now you want to copy that to another place on the page.  It doesn't seem unreasonable for PHP to be able to pull that out of the HTML tags into a variable, so you can emit it somewhere else in the HTML.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
OmniUnlimitedCommented:
That might seem reasonable, but it is not possible.  You would need to do it in reverse and set a PHP variable to the title and display that in as many places as you want.

This all goes to good planning of the web page beforehand.  When good foreplanning is not present, generally you end up having to go back in there and change coding.  In this case, it would require you to add a line of code and change the content of your <title> tags.
0
 
OmniUnlimitedCommented:
You would need to use javascript to acheive what you described.
0
 
Marco GasiFreelancerCommented:
Maybe you could create a script like this to grab the title:

<?php
$string = file_get_contents("urltopage.html");

preg_match('%(?<=<title>).*(?=</title>)%', $string, $matches);

$title = $matches[0];

echo $title;

?>
0
 
Ray PaseurCommented:
But still, suppose you had a bunch of pages where someone had already set the title between <title> tags and now you want to copy that to another place on the page.

This is a bizarre design pattern as others have suggested.  However you might try something like this:

Original document:
<title>Hello World</title>

Changed document
<title><?php $page_title = "Hello World"; echo $page_title; ?></title>

Now you can use the $page_title variable in any place you want inside the scope of your PHP script.

Best regards, ~Ray

PS: I don't recommend this; I just point out that it would work.  Instead I recommend changing the design pattern to something a little more mainstream.  Good luck!
0
 
stevaAuthor Commented:
Thanks.  These are all pretty painful solutions. (No reflection on the authors, just the situation.)  I started out looking for something simple like $_TITLE in the $_SERVER array. I mean, if there's a PHP routine to get meta tags it seemed like the value of the title tag might also be readily available.  But as all of us know now, there's nothing like that available in PHP. So  I changed the pages to define a $title variable to hold what's currently between the <title> tags and then emit that between the title tags and the other places I want it.  

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.