Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter自动链接在没有www的情况下不会添加链接_Php_Codeigniter_Autolink - Fatal编程技术网

Php codeigniter自动链接在没有www的情况下不会添加链接

Php codeigniter自动链接在没有www的情况下不会添加链接,php,codeigniter,autolink,Php,Codeigniter,Autolink,如果我打字 www.google.com或http://www.google.com 使用auto\u link()函数将正确添加链接 但是,如果我输入 google.com 由于缺少www.part,该功能无法运行 我如何确保它也包含这些链接 这是codeigniter的功能: /** * Auto-linker * * Automatically links URL and Email addresses. * Note: There's a bit of extra code h

如果我打字

www.google.com
http://www.google.com

使用
auto\u link()
函数将正确添加链接

但是,如果我输入

google.com
由于缺少www.part,该功能无法运行

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

这是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;
    }
}
/**
*自动连接器
*
*自动链接URL和电子邮件地址。
*注意:这里有一些额外的代码需要处理
*以句点结尾的URL或电子邮件。我们要剥掉这些
*关闭并在链接后添加它们。
*
*@access-public
*@param字符串字符串
*@param字符串类型:电子邮件、url或两者
*@param bool是否创建弹出链接
*@返回字符串
*/
如果(!function_存在('auto_link'))
{
函数自动链接($str,$type='both',$popup=FALSE)
{
如果($type!=“电子邮件”)
{

如果(preg|u match|u all(“#(^ |\s|\()((http(s?)/)|(www\)))(\w+[^\s\)\自动链接应该正确捕获。你是说这不起作用吗


autolink正则表达式使用http(s)或www来表示存在链接。如果没有这两个选项,您必须更改正则表达式以仅在.com的顶级域上检测,这将非常有问题,因为可能存在大量顶级域(.net、.org、.biz等)。如果您仔细考虑,您可能不想更改此正则表达式,因为对所有可能的域和正在添加的新域的维护将比它的价值要麻烦得多。

很抱歉,您使用的auto_link()是错误的

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

所以你应该:

auto_link('http://www.google.com')
auto_link('http://google.com')
不要一开始就使用“http://”。

如果你不使用“http://”或“www.”作为“链接此”触发器,你必须将其编码为catch.com、.org、.net(以及不断扩展的一组可能性)。正如其他人所建议的那样,使此注册表更“贪婪”“将匹配不应该是链接的内容。这取决于您根据优先级权衡平衡

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

<?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);
函数自动链接($str,$type='both',$popup=FALSE)
{
如果($type!=“电子邮件”)
{
如果(!preg_match_all(“/^([a-zA-Z0-9_。-])+@([a-zA-Z0-9_。-])+\。([a-zA-Z])+([a-zA-Z])+/”,$str,$matches)){
如果(preg_match_all(“/(?:https?\:?(?:\/)?\124; www\)?([a-zA-Z0-9\-\.]+.(?:[a-z]*)/mi“,$str,$matches))
{
$pop=($popup==TRUE)?“target=\”\u blank\:“”;
对于($i=0;$i

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

我是说,autolink不会捕获google.com,并且需要.or.www.才能工作。即使缺少www或http,如何修改regex使其工作在我的例子中?@fxuser您不需要修改正则表达式。只需使用url帮助程序中的
prep\u url()
函数,然后将
auto\u link()
函数应用于从
prep\u url()
返回的值。问题是,我有一堆可能包含url的文本……如果我先添加prep\u url(),然后再添加auto\u link()它只需在整个文本前面添加http://而不是在链接中……我没有你需要的即时正则表达式更改。正则表达式的整个逻辑更改,因为你不是从一个已知的开始字符串搜索到域的结尾,而是从结尾搜索并返回到前面。我真的不这么认为您需要它。您必须捕获顶级域的所有变体,这些变体最多只能维护起来很麻烦。您的逻辑有缺陷,无法强制
auto_link()
这样做。这会给你的
造成混乱,你最终会得到一些不应该有的文本的随机URL。即使如此,这里也没有像真正的链接那样雄心勃勃地格式化所有URL。想想看。你会为某个东西是电子邮件还是URL,或者只是一个带有句号的刺而争吵。J至少在我看来,不要把没坏的东西弄乱。
<?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;
}