DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Use The Current Filename As The HTML Body Id

06.11.2008
| 5562 views |
  • submit to reddit
        // Makes the body id = the current filename - extension
// Useful while working on static markup that relies on styles/scripts targeting the current page 

<?php
	$page = $_SERVER['PHP_SELF'];
	$page = str_replace("/","",$page);
	$page = str_replace(".php","",$page);
?>

<body id="<?php echo $page ?>">
    

Comments

Snippets Manager replied on Tue, 2008/06/10 - 4:11pm

True, that's better -- thanks :)

Snippets Manager replied on Sun, 2008/06/15 - 1:38pm

If i have a page att a url something like www.example.com/test/mypage/index.php this script would output would not something like <?php $page = basename($_SERVER['PHP_SELF'], ".php"); ?> be better ? do you want the extra "testmypage" in the id from the directory path ?