-4

如果我输入

www.google.com或者http://www.google.com

使用auto_link()功能将正确添加链接..

但是,如果我输入

google.com

自 www 以来,该功能不起作用。部分不见了……

我如何确保它也包含这些链接?

这是来自codeigniter的功能:

/**
 * Auto-linker
 *
 * Automatically links URL and Email addresses.
 * Note: There's a bit of extra code here to deal with
 * URLs or emails that end in a period.  We'll strip these
 * off and add them after the link.
 *
 * @access  public
 * @param   string  the string
 * @param   string  the type: email, url, or both
 * @param   bool    whether to create pop-up links
 * @return  string
 */
if ( ! function_exists('auto_link'))
{
    function auto_link($str, $type = 'both', $popup = FALSE)
    {
        if ($type != 'email')
        {
            if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
            {
                $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";

                for ($i = 0; $i < count($matches['0']); $i++)
                {
                    $period = '';
                    if (preg_match("|\.$|", $matches['6'][$i]))
                    {
                        $period = '.';
                        $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
                    }

                    $str = str_replace($matches['0'][$i],
                                        $matches['1'][$i].'<a href="http'.
                                        $matches['4'][$i].'://'.
                                        $matches['5'][$i].
                                        $matches['6'][$i].'"'.$pop.'>http'.
                                        $matches['4'][$i].'://'.
                                        $matches['5'][$i].
                                        $matches['6'][$i].'</a>'.
                                        $period, $str);
                }
            }
        }

        if ($type != 'url')
        {
            if (preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches))
            {
                for ($i = 0; $i < count($matches['0']); $i++)
                {
                    $period = '';
                    if (preg_match("|\.$|", $matches['3'][$i]))
                    {
                        $period = '.';
                        $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
                    }

                    $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
                }
            }
        }

        return $str;
    }
}

解决方案 : function auto_link($str, $type = 'both', $popup = FALSE) { if ($type != 'email') { if (!preg_match_all("/^([a-zA-Z0-9_. -])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/", $str, $matches) ){

        if (preg_match_all("/(?:https?\:?(?:\/\/)?|www\.)?([a-zA-Z0-9\-\.]+\.(?:.[a-z]*))/mi", $str, $matches))
        {
            $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";

            for ($i = 0; $i < count($matches['0']); $i++)
            {

                            $str = str_replace($matches[0][$i],
                                                '<a href="http://'.$matches[1][0].'" class="auto_link_color">'.$matches[1][0].'</a>', $str);
            }
        }
            }else{
                for ($i = 0; $i < count($matches['0']); $i++)
                {
                    $str = str_replace($matches[0][$i], $matches[0][0], $str);
                }
            }
    }

    return $str;
}

这解决了我的问题,因此用户输入的任何链接都会找到它并添加链接......即使用户输入电子邮件,它也不会在域部分添加链接,而是将其显示为文本。

4

4 回答 4

3

自动链接应正确捕获http://google.com。你说这行不通?

自动链接正则表达式使用 http(s) 或 www 的存在来指示存在链接。如果没有这些选项中的任何一个,您将不得不更改正则表达式以仅在 .com 的顶级域上检测,考虑到可能的顶级域(.net、.org、.biz 等)的广泛阵列,这将变得非常有问题。 )。如果您仔细考虑过,您可能不想更改此正则表达式,因为对所有可能的域和添加的新域的维护将比它的价值要麻烦得多。

于 2012-02-17T11:56:01.253 回答
1

很抱歉,您以错误的方式使用 auto_link()。

该参数应该是一个有效的 url 或电子邮件。只放“www.google.com”是不允许的,根本不应该工作:-)

所以你应该这样做:

auto_link('http://www.google.com')
auto_link('http://google.com')

切勿在开头省略“http://”。

于 2012-02-22T17:30:17.270 回答
0
function auto_link($str, $type = 'both', $popup = FALSE)
{
    if ($type != 'email')
    {
            if (!preg_match_all("/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+/", $str, $matches)){

        if (preg_match_all("/(?:https?\:?(?:\/\/)?|www\.)?([a-zA-Z0-9\-\.]+\.(?:.[a-z]*))/mi", $str, $matches))
        {
            $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";

            for ($i = 0; $i < count($matches['0']); $i++)
            {

                            $str = str_replace($matches[0][$i],
                                                '<a href="http://'.$matches[1][0].'" class="auto_link_color">'.$matches[1][0].'</a>', $str);
            }
        }
            }else{
                for ($i = 0; $i < count($matches['0']); $i++)
                {
                    $str = str_replace($matches[0][$i], $matches[0][0], $str);
                }
            }
    }

    return $str;
}

这解决了我的问题,因此用户输入的任何链接都会找到它并添加链接......即使用户输入电子邮件,它也不会在域部分添加链接,而是将其显示为文本。

于 2012-02-25T09:19:22.197 回答
0

如果您不使用“http://”或“www”。作为您的“链接此”触发器,您必须对其进行编码以捕获 .com、.org、.net(以及不断扩大的可能性)。正如其他人所建议的那样,使这个 reg ex “贪婪”将匹配不应该是链接的东西。您可以根据自己的优先级权衡天平。

这是我尝试的一个(非常简单的)小实验:

<?php

header("Content-type: text/plain");

$text = 'http://google.com
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in urna auctor tellus consequat cursus.
Cras malesuada magna eget felis accumsan vel dictum urna sodales. Vestibulum erat ante, rutrum et
aliquet vel, convallis eu google.com. Phasellus interdum velit at urna google.com porta. Donec at interdum
nibh. Fusce ultricies varius elit id egestas. Suspendisse dolor risus, vulputate vel rutrum in,
http://google.com et nisi. Etiam non massa non neque lacinia adipiscing sed nec metus. Sed fermentum ultricies
dui at porta. Duis at lacinia tortor. Nam mi est, mollis sed viverra et, mollis ac lorem. In mattis
lacinia tempor.

Sed in luctus nunc. Mauris nec tincidunt dui. Vivamus interdum, velit sed lobortis lobortis, nulla dui
vestibulum dui, eu tincidunt arcu felis et massa. google.COM vitae porta felis. Sed sit amet magna augue.
Aenean dignissim tempus porta. Donec ultrices lectus ac sapien gravida sodales. Quisque malesuada
sagittis rhoncus. Vestibulum mattis auctor ligula, eu tempus odio hendrerit in. Ut vel elit ipsum. Sed
ante lorem, www.google.com et dictum nec, ultricies a lorem.
';

$domains = 'com|org|net';

if ( !preg_match_all('#([\S]*)(\.('.$domains.']))#i', $text, $matches))
{
    die('no matches');
}

print_r($matches);

输出:

Array
(
    [0] => Array
        (
            [0] => http://google.com
            [1] => google.com
            [2] => google.com
            [3] => http://google.com
            [4] => google.COM
            [5] => www.google.com
        )

    [1] => Array
        (
            [0] => http://google
            [1] => google
            [2] => google
            [3] => http://google
            [4] => google
            [5] => www.google
        )

    [2] => Array
        (
            [0] => .com
            [1] => .com
            [2] => .com
            [3] => .com
            [4] => .COM
            [5] => .com
        )

    [3] => Array
        (
            [0] => com
            [1] => com
            [2] => com
            [3] => com
            [4] => COM
            [5] => com
        )

)
于 2012-02-23T17:19:40.490 回答