470,849 Members | 1,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Page Title

Is it possible to return the Page title in php ?

I can return the end of the url with $_SERVER["PHP_SELF"] however it
is the "Page Title" that I require.

Thanks
Jun 2 '08 #1
2 1349
Stuartsblog wrote:
Is it possible to return the Page title in php ?

I can return the end of the url with $_SERVER["PHP_SELF"] however it
is the "Page Title" that I require.
Not really. The Title is part of the response your script generates.
PHP_SELF is a value given by your Webserver and more representing the
request that cases your skript to run.

Heiko
Jun 2 '08 #2
On Thu, 15 May 2008 20:53:55 +0200, Stuartsblog
<st****@stuartstuart.fsnet.co.ukwrote:
Is it possible to return the Page title in php ?

I can return the end of the url with $_SERVER["PHP_SELF"] however it
is the "Page Title" that I require.
Not just like that. To PHP, all previous output is just a random string
for all it's concerned. If the page title of the current page is of
importance, you should know it earlier on, or at least save it. If you're
in the habit of just maxing PHP & HTML, teach yourself to prepare all data
in strings before any output is generated, and just ending it with a
outputting HTML and the previous data stored.

An illustration, if your page is kind of like this:
<html>
<head><title><?php echo some_function_output(); ?></title>
<body>
<?php
mysql_connect(...);
$r =mysql_query('SELECT data FROM table');
$data = mysql_fetch_assoc($r)
echo $data['data'];
//euhm, title change?
?>
</body>
</html>

Change it to this:

<?php
$title = some_functions_output();
mysql_connect(...);
$r =mysql_query('SELECT data FROM table');
$data = mysql_fetch_assoc($r)
$content = $data['data'];
//title change
$title .= 'output succeeded';
?>
<html>
<head><title><?php echo $title; ?></title>
<body>
<?php echo $content; ?>
</body>
</html>
If that cannot be done, or you need a quick fix, here is some majorly
dirty code I'd slap a previous coder for using:

<?php
ob_start();
function get_title(){
$send_so_far = ob_get_contents();
if(preg_match('@<title[^>]*>(.*?)</title>@si',$send_so_far,$match)){
return $match[1];
}
return false;
}
?>
<html>
<head><title>foobar</title>
<body>
<?php echo get_title(); ?>
</body>
</html>

--
Rik Wasmus
[SPAM] Now temporarily looking for some smaller PHP/MySQL projects/work to
fund a self developed bigger project, mail me at rik at rwasmus.nl. [/SPAM]
Jun 2 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Don Grover | last post: by
1 post views Thread by alex | last post: by
5 posts views Thread by Maxim Izbrodin | last post: by
6 posts views Thread by scottyman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.