Forum Moderators: coopster

Message Too Old, No Replies

Create <title>$variable</title> from php file

         

hOtTiGeR123

6:01 pm on Jun 9, 2008 (gmt 0)

10+ Year Member



Hi,

Say I have four php files, one called header, one called home, one called content and the 4th called footer.

Now, in the home.php I include the other 3 to make my web page. Now is there any way that using the name of the home.php it can create the title, so <title>Home</title>?

So whatever is the name of the php file with all the includes, it takes that filename and used that for its title tag.

Or is there another way that I can do all this?

I'd like to try and only use a header and footer once if possible. Sorry for the bad explanation.

Thanks

[edited by: hOtTiGeR123 at 6:01 pm (utc) on June 9, 2008]

StoutFiles

6:41 pm on Jun 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$title = "whatever you want it to be";
echo "<title>".$title."</title>";
?>
Carry the $title variable in the include. I'm not really sure what you mean though.

[edited by: StoutFiles at 6:41 pm (utc) on June 9, 2008]

d40sithui

6:49 pm on Jun 9, 2008 (gmt 0)

10+ Year Member



the key here is the php predefined variable named $_SERVER['PHP_SELF']
this guy tells you what script is currently running. one small problem though - it returns the relative url of the script. so if you're in the main web directory and in "home.php", you'd get "/home.php" or if you're in a subfolder, you'd get "/foldername/home.php". ideally, you'd want just "home." for this purpose, take a look at the "header.php" file below.

//header.php


<?
function getScriptName($url){
$name = "Undefined Page"; //default name
$pattern = "/\/[a-zA-Z0-9\_\-]+\.php/"; //finds file name based on extension
preg_match($pattern ,$url, $matches);

if(is_array($matches) && !empty($matches[0])){
$name = $matches[0]; //gets script name
}
$name = str_replace("/", "", $name); //replaces first slash
$name = preg_replace("/\.(php¦¦html)/", "", $name);//replaces the file extension

return $name; //returns name of script
}
?>

<html>
<head>
<title><? echo getScriptName($_SERVER['PHP_SELF']); ?> </title>
</head>

g1smd

8:35 pm on Jun 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have done similar things.

Sniff the filename (note that I always separate the words with dots).

Strip off the leading file path and the trailing extension (and any query strings).

Uppercase the first word, and change the dots to spaces.

Use that for the title tag.

hOtTiGeR123

9:49 pm on Jun 9, 2008 (gmt 0)

10+ Year Member



Thanks d40sithui, that brings up home.php in the title. (I got rid of the .php).

g1smd how would I go about doing that? I only want to capitalize the first letter and that would be great!

Cheers for your help

[edited by: hOtTiGeR123 at 9:51 pm (utc) on June 9, 2008]

mooger35

10:35 pm on Jun 9, 2008 (gmt 0)

10+ Year Member



Try ucwords [ca3.php.net]

or

ucfirst [ca3.php.net]

hOtTiGeR123

6:02 am on Jun 10, 2008 (gmt 0)

10+ Year Member



Sweet nice one ucwords worked a treat!

Thanks for all your help guys, much appreciated :)

eelixduppy

2:42 pm on Jun 10, 2008 (gmt 0)



Glad you found your solution, hOtTiGeR123! Welcome to WebmasterWorld! :)