codeigniter auto_link在没有www.的情况下不会添加链接


codeigniter auto_link doesnt add links without www

如果我键入

www.google.comhttp://www.google.com

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

但是,如果我键入

google.com

由于缺少www部分,该函数无法工作。。。

我怎样才能确保它也包括这些链接??

这是代码点火器的功能:

/**
 * 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;
    }
}

解决方案:函数auto_link($str,$type='both',$popup=FALSE){if($type!="电子邮件"){if(!preg_match_all("/^([a-zA-Z0-9_.-])+@

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

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

自动链接应正确捕获http://google.com.你是说那不管用吗?

自动链接regex使用http或www来指示存在链接。如果没有这两个选项,您将不得不更改regex,使其仅在.com的顶级域上进行检测,考虑到可能的顶级域(.net、.org、.biz等)的范围很广,这将非常有问题。如果您仔细考虑,您可能不想更改此regex,因为对所有可能的域和添加的新域的维护将比这是值得的。

抱歉,您使用auto_link()的方式不对。

参数应该是VALID url或电子邮件。只放"www.google.com"是不允许的,也不应该起作用:-)

所以你应该这样做:

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

千万不要在一开始就忽略"http://"。

如果您不使用"http://"或"www."作为"链接此"触发器,则必须将其编码为catch.com、.org、.net(以及不断扩展的一组可能性)。正如其他人所建议的,将这个reg ex设置为"greedier"将匹配不应该是链接的内容。这取决于你根据你的优先事项来权衡平衡。

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

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

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

相关文章: