我有以下结构...php preg_replace:找到链接并添加#hash到它?
$output = '<li><a href="http://forum.example.org">Something</a></li>'
其实$输出保存多个列表项。
对每个链接href应用#hash的最佳和最简单的方法是什么?在...
<li><a href="http://forum.example.org#something">Something</a></li>
不知道如何解决?
编辑:顺便说一句,它应该总是相同的#hash不像你在上面这个例子中想的那样,#something等于链接的名字。所以它应该是每个链接的东西。
add_filter('wp_list_pages', 'add_hash'); /*Add #hash to wp_list_pages() function*/
function add_hash($output) {
$dom = new DOMDocument();
$dom->loadHTML($output);
$a_tags = $dom->getElementsByTagName('a');
foreach($a_tags as $a)
{
$value = $a->getAttribute('href');
$a->setAttribute('href', $value . '#b');
}
$dom->saveHTML();
return $output;
}
你应该尽量适应名单代插入的代码#hashes而不是修改连接的结果。 – mario 2011-03-15 20:37:50