Get the name of the current file with php

From The IT Community
Jump to: navigation, search




If you have a website written in php and you want to get the name of the current page, it is actually very easy.

Just use

basename(preg_replace('/\.php$/', , __FILE__))


Let´s brake this down:

 __FILE__

gives you the name of the current file including .php and the full path.


To get rid of the extension we use

(preg_replace('/\.php$/', , __FILE__)

which replaces the .php with nothing.


Now, to get rid of the full path, we use the php function basename.


If you - for example - want to replace the keywords meta tag with the name of the current page you can insert the following into your head section:

<meta name="keywords" content="<?php echo basename(preg_replace('/\.php$/', , __FILE__)) ?>" />


This is great if you want to create dynamically created sites.



Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.






Translate this page:




Articles found in the same category:
(max. 20 shown)