Php 使用hashtag创建url路径

Php 使用hashtag创建url路径,php,kohana,Php,Kohana,这是我第一次这样做,所以我不知道如何开始,也没有我尝试过的示例代码 基本上,我需要根据用户在标签(#)后键入的数字创建url路径,这与facebook和twitter的做法大致相同 例如,如果我的url类似于www.example.come/result/2657。假设链接是一个评论框,你可以在这个评论框中输入评论,在这个评论框中你想链接到另一个页面,你所要做的就是用hashtah加上数字。e、 g.“此测试结果相同。”。现在,如果你点击“#2657”,这将带你进入www.example.com

这是我第一次这样做,所以我不知道如何开始,也没有我尝试过的示例代码

基本上,我需要根据用户在标签(#)后键入的数字创建url路径,这与facebook和twitter的做法大致相同

例如,如果我的url类似于
www.example.come/result/2657
。假设链接是一个评论框,你可以在这个评论框中输入评论,在这个评论框中你想链接到另一个页面,你所要做的就是用hashtah加上数字。e、 g.“
此测试结果相同。”。现在,如果你点击
“#2657”
,这将带你进入
www.example.come/result/2657`

请记住,它并不总是
2657
,有时也可能是
www.example.come/admin/result/7485
www.example.come/host/result/6475


我以前从未做过这件事,我不知道如何忍受它,所以我没有任何我尝试过的示例代码,因为我不知道什么时候开始。我在谷歌上找不到任何东西。如果你知道,请告诉我正确的方向。

我相信这就是你想要的

Javascript版本(独立于JQuery)
var str=document.getElementById(“内容”).innerHTML;
var rx=/#\d{1,5}/g;
str=str.replace(接收,功能($0){
$0=$0.分割(“#”);
$0 = $0[1];
返回“”;
});
document.getElementById(“内容”).innerHTML=str;

PHP版本
你的问题没有多大意义。您应该添加相关代码和您尝试过的内容。@MilindAnantwar我确实说过我以前没有做过,也不知道如何开始它。您至少应该重新构造问题,以便等待回答的任何人都可以付出努力并提供帮助。@MilindAnantwar为我的语法错误感到抱歉。我已经对它进行了重组,希望这对Hanks Ergec有所帮助,但这是一种仅使用php的方法吗?Ergec非常感谢。我真的很感谢你的帮助和你的时间
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;
?>