我有以下結構...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