Re: dynamic title tag generation?
| From: | Justin French | Date: | Sat, 14 Sep 2002 02:21:30 +0000 |
| Subject: | Re: dynamic title tag generation? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-116343@lists.php.net to get a copy of this message | ||
I think you can get what you want with the variables $_SERVER['PHP_SELF']
and $_SERVER['HTTP_HOST'].
<?
echo $_SERVER['HTTP_HOST']; // something like domain.com
echo $_SERVER['PHP_SELF']; // something like /cgi/tut.four.php
?>
It WILL depend on your server's set-up as to IF these vars a filled, and
WHAT they are filled with, but in my case, if I wanted a title on every page
that was assessed out of the URL in the form
<domain> - <dir> - <dir> - ... - <file>
I'd do this:
<?
$title = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
// domain.com/cgi/tut.four.php for example
$title = str_replace('/', ' - ', $title)
// domain.com - cgi - tut.four.php for example
?>
Then from here, everything is optional, and totally dependant on your code,
naming conventions, etc etc. Here's a better example:
<?
$domain = $_SERVER['HTTP_HOST'];
$phpself = $_SERVER['PHP_SELF'];
$phpself = str_replace('.php', '', $phpself); // remove .php
$phpself = str_replace('.', ' ', $phpself); // remove .'s
$phpself = str_replace('tut', 'tutorial', $phpself);
$phpself = str_replace('cgi', 'CGI', $phpself);
$phpself = ucwords($phpself);
$title = $domain.$phpself;
echo "<TITLE>{$title}</TITLE>";
?>
Given the URL domain.com/cgi/tut.four.php, the title should now be:
<TITLE>domain.com - CGI - Tutorial Four</TITLE>
The only thing I haven't given you above is a function to convert written
numbers into numerals (eg four > 4, fourteen > 14).
I'm not really sure if a function exists for that.
Please note, all the above is untested code, but should be cool.
Happy hacking,
Justin
on 14/09/02 11:52 AM, eriol (eriol@eriol.com) wrote:
> Allo..
>
> I have a small CMS that I'm in the process of creating and was wondering how I
> would achieve <title> tags that match the directory name & file name the
> visitor is viewing dynamically.. I'm a newbie, so please bear with me.. I have
> a head.php file which has the following line:
>
> echo "<title> DOMAIN - ? - ? </title>\n";
>
> The head.php is included in each and every file displayed thru-out my site and
> has all the obvious opening html, head & opening body tags.. In place of the
> ?'s in the title tag above, I would like to dynamically change this.. For
> example, say I have a file located at the following URL:
> http://domain.com/php/tut.one.php
>
> I want to be able to have the title tag for that specific page read:
> DOMAIN - PHP - Tutorial 1
>
> Yet if I have a file located here:
> http://domain.com/cgi/tut.four.php
>
> I would want this to be output for this specific page's title tag:
> DOMAIN - CGI - Tutorial 4
>
> It's kind of similar to hotscripts.com and how their directory set up and
> their title tag corresponds to the directory names, but I don't want a
> directory structure as my title tag..
>
> As my head is the first included file following the opening <?php tag, I
> wasn't sure how to go about this.. If I created a variable such as $title =
> "DOMAIN - PHP - Tutorial 4"; for each page title, the head would be read (and
> I assume parsed) before the actual variable.. I could put the $title variable
> above the include("head.php"); line, but it creates more hassle editing each
> file to add a $title variable and seems like somewhat sloppy coding.. This is
> also why I don't name my files specifically how I want the title tag to
> display them and use $request_uri or some similar reserved variable.. Is there
> an easy way to create a function to read the directory and file names and have
> it changed to display how I have my desired title tag output examples above?
>
> If someone could point me to the correct function() names and how to combine
> them if it's not obvious in the manual, I'd really appreciate it.. I don't
> need the manual's URLs, I can search the manual and don't want someone else
> doing my work for me.. I'll attempt to build the function myself, but a small
> example would be nice as I'm still learning and not sure how to combine all
> functions using the correct and most efficient syntax.. Similarly, if you've
> happen to come across any tutorials aside from php.net that cover this, please
> share the URL(s).. I've searched google, hotscripts and resourceindex among
> other places, but can't seem to find what I'm looking for.. TIA.. Hope I made
> sense..
>
> Take care.. peace..
> eriol
>
>
>