Getting the page title to a PHP variable

Posted on 2011-09-05
Last Modified: 2012-05-12
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.

Question by:steva
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 17

Accepted Solution

OmniUnlimited earned 167 total points
ID: 36484688
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:

$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.
LVL 31

Expert Comment

by:Marco Gasi
ID: 36484698
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?

Author Comment

ID: 36484779
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.
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

LVL 17

Expert Comment

ID: 36484794
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.
LVL 17

Expert Comment

ID: 36484850
You would need to use javascript to acheive what you described.
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 167 total points
ID: 36484855
Maybe you could create a script like this to grab the title:

$string = file_get_contents("urltopage.html");

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

$title = $matches[0];

echo $title;

LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 36485380
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!

Author Comment

ID: 36486189
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.  


Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question