php在当前页面的UL LI中添加类到Link

时间:2013-04-13 07:08:41

标签: php html regex

在php上使用regex尝试在发送到浏览器之前将“curPage”类添加到我的ul-li菜单中。 代码:

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

$ 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>

2 个答案:

答案 0 :(得分:3)

解析HTML文本非常容易出错,就像你正在做的那样几乎没有 -

最好使用DOM parser来解析和修改HTML,如下代码:

$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();

输出:

<!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>

答案 1 :(得分:0)

在命令行上执行此代码可以正常工作。那部分不起作用的东西对我们来说是隐藏的。

<?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>

?>

也许loadedMenu的初始值有单引号而不是双引号或href值略有不同。

您可以使用稍微更通用的正则表达式,捕获任何文件名而不是代码中的特定文件......

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