-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

「http://」または「www」を使用していない場合。「これをリンクする」トリガーとして、.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);

出力:

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 に答える
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 に答える