Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting the page title to a PHP variable

Posted on 2011-09-05
8
Medium Priority
?
291 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
[X]
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
8 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 668 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 668 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

618 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