Mike
Mike

Reputation: 799

php add class to Link in UL LI of current page

trying with regex at php to add a "curPage" class to my ul-li menu before sending to browser. code:

$loadedMenu=preg_replace("/a href=\"" . $file . ".htm\"/", "a href=\"" . $file . ".htm\" class=\"curPage\"", $loadedMenu);

content of $loadedMenu:

<nav><ul><li rel="file" id="516054b57fbba"><a href="index.htm">דף הבית</a></li><li rel="file" id="51681f81a440b"><a href="newuser.htm">משתמש חדש</a></li><li rel="file" id="516054b57fb40"><a href="about.htm">אודות</a></li><li rel="file" id="5160f37b822a3"><a href="%D7%93%D7%A3%20%D7%97%D7%93%D7%A9.htm">דף חדש</a></li><li rel="folder" id="516054d162176"><a href="#" style="cursor:text;">תיקייה חדשה</a><ul><li rel="file" id="516054b57fc62"><a href="%D7%9E%D7%99%D7%98%D7%9C%20%D7%94%D7%A0%D7%A1%D7%99%D7%9B%D7%94%20%D7%A9%D7%9C%D7%99.htm">מיטל הנסיכה שלי</a></li><li rel="file" id="516054b57fc9a"><a href="%D7%A0%D7%A1%D7%99%D7%95%D7%9F.htm">נסיון</a></li><li rel="file" id="516054b57fb82"><a href="help.htm">עזרה</a></li></ul></li><li rel="folfil" id="5160552162177"><a href="%D7%AA%D7%99%D7%A7%D7%99%D7%99%D7%AA%20%D7%AA%D7%95%D7%9B%D7%9F.htm">תיקיית תוכן</a><ul><li rel="file" id="516054b57fbf2"><a href="test.htm">test</a></li></ul></li><li rel="file" id="516054b57fc2a"><a href="%D7%92%D7%9C%D7%99%D7%93%D7%94.htm">גלידה</a></li><li rel="file" id="516054b57fcd2"><a href="%D7%A0%D7%A1%D7%99%D7%95%D7%9F0.htm">נסיון0</a></li></ul></nav>

Upvotes: 1

Views: 578

Answers (2)

Billy Moon
Billy Moon

Reputation: 58541

Executing this code on the command line works fine. That part that does not work is hidden from us.

<?php

$file = "index";
$loadedMenu = '<a href="'.$file.'.htm">whatever</a>';

$loadedMenu=preg_replace("/a href=\"" . $file . ".htm\"/", "a href=\"" . $file . ".htm\" class=\"curPage\"", $loadedMenu);

echo $loadedMenu;
// <a href="index.htm" class="curPage">whatever</a>

?>

Perhaps the initial value of loadedMenu has single quotes instead of double, or a slightly different href value.

You could use a slightly more generic regex, capturing any filename instead of the specific file in your code...

$loadedMenu=preg_replace('/a href="(.+?)"/', 'a href="$1" class="curPage"', $loadedMenu);

Upvotes: 0

anubhava
anubhava

Reputation: 785256

It is so much error prone to parse HTML text like you're doing that it is almost no-no.

Better to use DOM parser to parse and modify HTML like this code:

$file = 'foo.htm'; // set your value here
# fetch your HTML content here
$html = <<< EOF
<html>
<a href="http://example.com/foo.htm">Click link1</a> morestuff
<a href="http://www.example.com/foo/baz">Click www.example.com</a> morestuff
<a href="http://notexample.com/foo/bar">notexample.com</a> morestuff
<a href="http://example.com/foo.htm">Click link1</a>
</html>
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
// find all hrefs with $file in it
$nodelist = $xpath->query("//a[contains(@href, '" . $file . "')]");
// iterate thru found links
for($i=0; $i < $nodelist->length; $i++) {
    $node = $nodelist->item($i);
    # add class attribute to them
    $node->setAttribute('class', 'curPage');
}
echo $doc->saveHTML();

OUTPUT:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><a href="http://example.com/foo.htm" class="curPage">Click link1</a> morestuff
<a href="http://www.example.com/foo/baz">Click www.example.com</a> morestuff
<a href="http://notexample.com/foo/bar">notexample.com</a> morestuff
<a href="http://example.com/foo.htm" class="curPage">Click link1</a>
</body></html>

Upvotes: 3

Related Questions