Jump to content

PREG_MATCH, PHP_SELF and adding tag elements


maxert

Recommended Posts

Hello,

I have a two level submenu that looks something like this:

[code]
<ul>
<li><a href="/main-dir/subdir1/>1st Item</a></li>
<li><a href="/main-dir/subdir2/>2nd Item</a>
    <ul>
        <li><a href="/main-dir/subdir2/a.php>1st SubItem</a>
        <li><a href="/main-dir/subdir2/b.php>2nd SubItem</a>
        <li><a href="/main-dir/subdir2/c.php>3rd SubItem</a>
    </ul>
</li>
<li><a href="/main-dir/subdir3/>3rt Item</a></li>
<li><a href="/main-dir/subdir4/>4th Item</a></li>
</ul>
[/code]

I am indicating the active page by giving the relevant <li> a class of "active"
The tricky part is that, in the case of SubItems I would like the parent <li> to have a class of "active" as well as the actual active page <li>.

I have some code that is working on the main menu:

[code]
//Menu is $menu
$lines = split("\n", $menu);
foreach ($lines as $line) {
    $current = false;
    preg_match('/href="([^"]+)"/', $line, $url);
    if (substr($_SERVER["PHP_SELF"], 0, 5) == substr($url[1], 0, 5)) {
        $line = str_replace('<a h', '<a class="active" h', $line);
        }
    echo $line."\n";
[/code]

Can I modify this to work for my submenu?

Thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.