Showing current page

Here's what I'm talking about - the menu shows what page you're on - and here's a great way to do this with PHP.

First, save your menu in an external file called 'menu.htm'.

Here's mine:

<ul id="menu">
<li><a href="index.php">Home</a></li>
<li><a href="one.php">Page One</a></li>
<li><a href="two.php">Page Two</a></li>
<li><a href="three.php">Page Three</a></li>
<li><a href="four.php">Page Four</a></li>
<li><a href="five.php">Page Five</a></li>

Then use this preg_replace code to take your menu, find the link to the current page, remove the 'a' tags and give the 'li' tag a class ('current') and print your menu on the page.

$menu=preg_replace("|<li><a href=\"".$base."\">(.*)</a></li>|U", "<li class=\"current\">$1</li>", $menu);
echo $menu;

It's that simple.

(This can also be done without php by using the cascade).


2006-11-28 Jason says :

do u think u could maybe explain a little more what all those symbols and codings do in the PHP file...
im sh00ter555 from htmlForums.

2006-12-28 n00b says :

first of all i must say thank you for such a wonderful article. I'm a n00b with Ajax, JS and this article has helped me get started. I had a question, is it possible to indicate the current page i.e. when a user is on that page indicate current page by changing its color. I know you have the php/css tutorial but is it possible for you to add this an Ajax/JS tutorial - Thanks.

2008-10-31 luna says :

I'm really a newbie here, so sorry if this is a dumb question..

I'm wondering how you edit the text that's displayed on the separate php pages. (e.g. Pageonethisispageone) Is it just part of the file itself like here? Or another separate file? I'm a little confused.


2009-02-12 Ellen says :

I have used php to identify the current page on my main menu.

I'm guessing there is no way to use the php to do it with a submenu link that only goes to a <div id=#submenu1"> on the page without reloading?

I'm trying to figure out how to mark a submenu item current if there is no javascript.

2010-08-18 Luis says :

On #7:

This is what I would do, Ellen.

I would use the PHP code shown here on the submenu elements. Then, to identify the parent of the current submenu I would use PHP to ask if the current page is in the array of pages (in_array) to make "current" the element of the main menu.

Let's see. This is the main menu.

Presudocode (main menu):
For ITEM1. if current_page in_array (i1_page1.php, i1_page2.php) then echo "current"

For ITEM2. if current_page in_array (i2_page1.php, i2_page2.php, i2_page3) then echo "current"

... an so on.

This would work only on a short list of menu items and subitems. Bigger menus would make maintenance difficult.

2011-11-13 Kang Sawul says :

I'm still not understand for this:
if current_page in_array (i1_page1.php, i1_page2.php) then echo "current"
"where mus I place that code?" can you explain more?

Comment form

Please type the word 'coffee' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url