1

在发送到浏览器之前,尝试在 php 中使用正则表达式将“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>
4

2 回答 2

3

像您正在做的那样解析 HTML 文本非常容易出错,这几乎是不可能的。

最好使用DOM 解析器来解析和修改 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>
于 2013-04-13T07:45:11.943 回答
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);
于 2013-04-13T07:45:34.397 回答