SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Twist on Highlight Current Page Link

    Hi, PHP relative newbie here. I have used the following code:

    Code:
    $menu=file_get_contents("nav.php");
    $base=basename($_SERVER['PHP_SELF']);
    $menu=preg_replace("|<li><a href=\"".$base."\">(.*)</a></li>|U", "<li class=\"current\">$1</li>", $menu);
    echo $menu;
    obtained from elsewhere (I give full credit to whoever created it) and it works good to highlight the link of the page the visitor is currently on. Actually it even strips it of its link-like nature altogether.

    Now I am trying to adapt this script to work with a menu that has subnavigation, like so (using list-item "Asian" as our working example):

    Code:
    <li><a href="eating-out-asian.php">Asian</a>
    	<ul id="asianSub">
    		<li><a href="eating-out-asian.php#Chinese">Chinese</a></li>
    		<li><a href="eating-out-asian.php#Japanese">Japanese</a></li>
    		<li><a href="eating-out-asian.php#Korean">Korean</a></li>
    		<li><a href="eating-out-asian.php#Thai">Thai</a></li>
    		<li><a href="eating-out-asian.php#Vietnamese">Vietnamese</a></li>
    		<li><a href="eating-out-asian.php#Other">Other</a></li>
    	</ul>
    </li>
    (Keep in Mind that the "Asian" list item is only one LI in the main navigation.)

    So far I have adapted the script to:

    Code:
    $menu=file_get_contents("subnavEO.php");
    $base=basename($_SERVER['PHP_SELF']);
    $menu=preg_replace("|<li><a href=\"".$base."\">|U", "<li class=\"current\">", $menu);
    echo $menu;
    Which works good to get rid of the opening A tag, but I haven't been able to figure out yet how to get rid of the respective closing A tag without also getting rid of all the closing A tags in the whole list.

    I'm going to keep trying to figure out a solution but if anybody else can before I do the help is appreciated in advance.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Code:
    $menu=file_get_contents("subnavEO.php");
    $base=basename($_SERVER['PHP_SELF']);
    $menu=preg_replace("|<li><a href=\"".$base."\">(.*?)</a></li>|U", "<li class=\"current\">$1</li>", $menu);
    echo $menu;
    Note the ? in the (.*?), this makes the expression non-greedy

    For more info on non-greedyness of regular expression see here
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, that kind of helps. Still not entirely sure what to do though. Wait . . . maybe I got it. Lemme see...

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So far that seems to highlight list items that don't have subnavs but doesn't highlight those list items that do.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •