Solved

Getting the page title to a PHP variable

Posted on 2011-09-05
8
282 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 30

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
 
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36484850
You would need to use javascript to acheive what you described.
0
 
LVL 30

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 108

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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.  …
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now