basename(): Given a string containing a path to a file, this function will return the base name of the file.
STEPS:
1. Create a new PHP file and insert the following code between a pair of PHP tags
echo basename($_SERVER['SCRIPT_NAME'], '.php');
2.Save the page with any name you like (as long as it has a .php filename extension),
and load it into a browser. It should display the name of the file stripped of the
.php extension. The download file displays scriptname2.
You now have the basis for automatically creating the page title for every page in
your site, using basename(), $_SERVER['SCRIPT_NAME'], and an include file.
3. Create a new PHP file called title.inc.php and save it in the includes folder.
4. Strip out any code inserted by your file, and type in the following code
(the finished code for title.inc.php is in the test/includes folder of the download
files):
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
?>
This finds the filename of the current page, strips the .php filename extension, and
assigns the result to a variable called $title.
Note: The code for this include file must be enclosed in PHP tags. This is because the whole file needs to be treated as PHP. Unlike the menu, it won’t be displayed directly inside other pages.
5. Open a PHP page in your script editor. If you’re using the test site, use
testcontact.php. Include title.inc.php by typing this above the DOCTYPE declaration:
6. Amend the "<title>" tag like this:
<title> My Test Php website </title>
This uses echo to display the value of $title. Because the string is enclosed in double quotes, PHP displays the value of $title
The variable $title has also been enclosed in curly braces because there is no
space between the em dash and $title. Although not always necessary, it’s a good
idea to enclose variables in braces when using them without any whitespace in a
double-quoted string, as it makes the variable clear to you and the PHP engine.
7. Save both pages and load the web page into a browser.
8. if you prefer an initial capital letter for the part of the title derived from the filename? Nothing could be simpler. PHP has a neat little function called ucfirst(), which does exactly that (the name is easy to remember once you realize that uc stands for “uppercase”). Add another line to the code in
step 4 like this:
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
$title = ucfirst($title);
?>
9. A drawback with this technique is that filenames consist of only one word—at least
they should. If you’ve picked up bad habits from Windows and Mac OS X permitting
spaces in filenames, get out of them immediately. Spaces are not allowed in
URLs, which is why most web design software replaces spaces with %20. You can get
around this problem, though, by using an underscore. Change the name of the file
you’re working with so that it uses two words separated by an underscore. For
example, change contact.php to contact_us.php.
10. Change the code in title.inc.php like this:
$title = basename($_SERVER['SCRIPT_NAME'], '.php');
$title = str_replace('_', ' ', $title);
$title = ucwords($title);
?>
12. Save title.inc.php and load into a browser the file that you renamed
Tuesday, February 3, 2009
Automatically generating a page’s title from filename
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment