使用hashtag创建url路径

时间:2022-05-11 10:24:55

This is my first time having to do this so I have no idea how to start and no example code of what I have tried.

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

Basically I need to create a url path depending on the number the user types after a hashtag(#) more or less the same thing that facebook and twitter does.

基本上我需要创建一个url路径,具体取决于用户在主题标签(#)之后键入的数字或多或少与facebook和twitter相同的内容。

For example If my url was like www.example.come/result/2657. lets say link as a comment box which you can type in comment and in this comment box you wanted to link to another page and all you have to do is hashtah follow by the numbers. e.g. "This test result is the same#2657". now if you click on'#2657'this will take you to thewww.example.come/result/2657`.

例如,如果我的网址是www.example.come / result / 2657。让我们说链接作为评论框,你可以在评论中输入,在这个评论框中你想链接到另一个页面,所有你要做的就是hashtah跟随数字。例如“这个测试结果是相同的#2657”。现在,如果你点击'#2657,这将带你到www.example.come / result / 2657`。

Bare in mind it will not always be 2657 also sometimes it might be www.example.come/admin/result/7485 or www.example.come/host/result/6475.

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

I have never done this before and I have no idea how to stand this so I don't have any example code which I have try because I have no idea how when to begin. I cant find anything on google. Please if you know, point me in the right direction.

我之前从未这样做过,我不知道如何忍受这个,所以我没有任何我尝试过的示例代码,因为我不知道何时开始。我在谷歌上找不到任何东西。如果你知道,请指出我正确的方向。

1 个解决方案

#1


0  

I believe this is what you're after

我相信这就是你所追求的

Javascript Version (JQuery independent)

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;

JSfiddle Example

JSfiddle示例

PHP Version

<?
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;
?>

#1


0  

I believe this is what you're after

我相信这就是你所追求的

Javascript Version (JQuery independent)

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;

JSfiddle Example

JSfiddle示例

PHP Version

<?
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;
?>