Solved

Getting the page title to a PHP variable

Posted on 2011-09-05
8
285 Views
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.

Steve
0
Comment
Question by:steva
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 17

Accepted Solution

by:
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:
 
<?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
 
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?
0
 

Author Comment

by:steva
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 17

Expert Comment

by:OmniUnlimited
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.
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36484850
You would need to use javascript to acheive what you described.
0
 
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:

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

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

$title = $matches[0];

echo $title;

?>
0
 
LVL 109

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!
0
 

Author Comment

by:steva
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.  

Thanks.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

821 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