这是我第一次这样做,所以我不知道如何开始,没有我尝试过的示例代码。
基本上我需要创建一个url路径,具体取决于用户在主题标签(#)之后键入的数字或多或少与facebook和twitter相同的内容。
例如,如果我的网址是www.example.come/result/2657
。让我们说链接作为评论框,你可以在评论中输入,在这个评论框中你想链接到另一个页面,所有你要做的就是hashtah跟随数字。例如“This test result is the same
#2657 ". now if you click on
'#2657'this will take you to the
www.example.come / result / 2657`。
请注意,有时2657
或www.example.come/admin/result/7485
不一定www.example.come/host/result/6475
。
我之前从未这样做过,我不知道如何忍受这个,所以我没有任何我尝试的示例代码,因为我不知道何时开始。我在谷歌上找不到任何东西。如果你知道,请指出我正确的方向。
答案 0 :(得分:0)
我相信这就是你追求的目标
var str = document.getElementById("content").innerHTML;
var rx = /#\d{1,5}/g;
str = str.replace(rx, function ($0) {
$0 = $0.split("#");
$0 = $0[1];
return "<a href='/my/path/to/link/" + $0 + "'>#" + $0 + "</a>";
});
document.getElementById("content").innerHTML = str;
<?
function preg_callback_hash_to_link($matches) {
return "<a href='/my/path/to/link/" . $matches[1] . "'>#" . $matches[1] . "</a>";
}
function hashtolink($html) {
$pattern = "/(?:#)(\d{1,5})/i";
return preg_replace_callback($pattern, 'preg_callback_hash_to_link', $html);
}
$content = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of 'de Finibus Bonorum et Malorum' (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, 'Lorem ipsum dolor sit amet..', comes from a line in section 1.10.32. #2276 The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from 'de Finibus Bonorum et Malorum' by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. #6578";
$content = hashtolink($content);
print $content;
?>