在重定向用户之前捕获外部链接

发布于 2024-11-23 16:14:43 字数 1008 浏览 0 评论 0 原文

我需要让我的用户知道他们正在单击外部链接。我的网站上有很多类型的聊天,有时用户会发布链接,这些链接可能对他们来说很危险,所以我想在离开网站之前警告他们。

例如,eveonline.com 在其论坛上使用以下内容: http://www.eveonline.com/externalLink.aspx?l=http://altdevblogaday.com/2011/07/11/the-hidden-evil-of-the-micro-transaction/

每当出现链接时,他们都会查看该域名是否与 eveonline.com 不同,如果是,他们会在其中添加“http://www.eveonline.com/externalLink.aspx?l=”。

这是我的 makeClickableLinks 函数,我用它来使链接可点击,我想知道是否有人可以重写我是否可以执行上述+可点击操作,因为我没有编写此函数,因为我对 preg_match 一无所知。

function makeClickableLinks($text)
    {
    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a target="_blank" href="\\1">\\1</a>', $text);

    $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a target="_blank" href="http://\\2">\\2</a>', $text);


    return $text;
    }

变量 $text 是用户的帖子。

i need to make my users aware that they are clicking an external link. I have many sorts of chats on my website and sometimes users post links, these links could be dangerous for them so i wanted to warn them before leaving the site.

For example eveonline.com uses the following on their forums: http://www.eveonline.com/externalLink.aspx?l=http://altdevblogaday.com/2011/07/11/the-hidden-evil-of-the-micro-transaction/

whenever a link shows up they see if the domain is different than eveonline.com and if it is they add the "http://www.eveonline.com/externalLink.aspx?l=" to it.

This is my makeClickableLinks function, i use it to make links clickable, i was wondering if someone could rewrite if for me to do the above + clickable as i didnt write this function since i am clueless with preg_match.

function makeClickableLinks($text)
    {
    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a target="_blank" href="\\1">\\1</a>', $text);

    $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a target="_blank" href="http://\\2">\\2</a>', $text);


    return $text;
    }

the variable $text is the post of the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2024-11-30 16:14:43

我不是 PHP 专家,但请尝试一下。请注意,几乎所有内容都直接取自 此处

function makeClickableLinks($text)
{
    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                          '<a target="_blank" href="\\1">\\1</a>', $text);      

    $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                          '\\1<a target="_blank" href="http://\\2">\\2</a>', 
                          $text);

    $regex = '@(<a.*?href=")((?!(?:(?:f|ht)tps?://)?(?:[a-z0-9]+\.)?domain\.com)[^"]*)@i';
    $replacement = '$1http://your.domain.com/externLink.php?l=$2';

    $text = preg_replace($regex, $replacement, $text);

    return $text;
}

I'm no PHP wizard, but give this a try. Note that almost all of this is taken straight out of the PHP manual here.

function makeClickableLinks($text)
{
    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                          '<a target="_blank" href="\\1">\\1</a>', $text);      

    $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                          '\\1<a target="_blank" href="http://\\2">\\2</a>', 
                          $text);

    $regex = '@(<a.*?href=")((?!(?:(?:f|ht)tps?://)?(?:[a-z0-9]+\.)?domain\.com)[^"]*)@i';
    $replacement = '$1http://your.domain.com/externLink.php?l=$2';

    $text = preg_replace($regex, $replacement, $text);

    return $text;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文