0

我正在寻找旅行自动链接检测。

我正在尝试制作一个社交媒体网站,当我的用户发布 URL 时,我需要它,就像节目一样,而不仅仅是普通的文本。

4

2 回答 2

0

试试dwightwatsonLaravel提供的 Autologin ,它可以让您生成 URL,这些 URL 将为您的应用程序提供自动登录,然后重定向到适当的位置

于 2017-04-01T05:38:14.600 回答
0

据我所知,Laravel 的核心中没有与 Code Igniter 的 auto_link() 函数帮助器等效的东西(假设您指的是 CI 版本)。

无论如何,获取该代码并在 Laravel 中使用它来快速解决问题非常简单。我只是随便寻找同样的问题。

在您的 App 目录中为您的助手(或任何容器,它只需要由框架发现)放置一个容器类,在这种情况下,我放置了一个 UrlHelpers.php 文件。然后,在其中放入了为 CI 版本抓取的两个静态函数:

class UrlHelpers
{
    static function auto_link($str, $type = 'both', $popup = FALSE)
    {
        // Find and replace any URLs.
        if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
            // Set our target HTML if using popup links.
            $target = ($popup) ? ' target="_blank"' : '';

            // We process the links in reverse order (last -> first) so that
            // the returned string offsets from preg_match_all() are not
            // moved as we add more HTML.
            foreach (array_reverse($matches) as $match) {
                // $match[0] is the matched string/link
                // $match[1] is either a protocol prefix or 'www.'
                //
                // With PREG_OFFSET_CAPTURE, both of the above is an array,
                // where the actual value is held in [0] and its offset at the [1] index.
                $a = '<a href="' . (strpos($match[1][0], '/') ? '' : 'http://') . $match[0][0] . '"' . $target . '>' . $match[0][0] . '</a>';
                $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
            }
        }

        // Find and replace any emails.
        if ($type !== 'url' && preg_match_all('#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i', $str, $matches, PREG_OFFSET_CAPTURE)) {
            foreach (array_reverse($matches[0]) as $match) {
                if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE) {
                    $str = substr_replace($str, static::safe_mailto($match[0]), $match[1], strlen($match[0]));
                }
            }
        }

        return $str;
    }

    static function safe_mailto($email, $title = '', $attributes = '')
    {
        $title = (string)$title;

        if ($title === '') {
            $title = $email;
        }

        $x = str_split('<a href="mailto:', 1);

        for ($i = 0, $l = strlen($email); $i < $l; $i++) {
            $x[] = '|' . ord($email[$i]);
        }

        $x[] = '"';

        if ($attributes !== '') {
            if (is_array($attributes)) {
                foreach ($attributes as $key => $val) {
                    $x[] = ' ' . $key . '="';
                    for ($i = 0, $l = strlen($val); $i < $l; $i++) {
                        $x[] = '|' . ord($val[$i]);
                    }
                    $x[] = '"';
                }
            } else {
                for ($i = 0, $l = strlen($attributes); $i < $l; $i++) {
                    $x[] = $attributes[$i];
                }
            }
        }

        $x[] = '>';

        $temp = array();
        for ($i = 0, $l = strlen($title); $i < $l; $i++) {
            $ordinal = ord($title[$i]);

            if ($ordinal < 128) {
                $x[] = '|' . $ordinal;
            } else {
                if (count($temp) === 0) {
                    $count = ($ordinal < 224) ? 2 : 3;
                }

                $temp[] = $ordinal;
                if (count($temp) === $count) {
                    $number = ($count === 3)
                        ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
                        : (($temp[0] % 32) * 64) + ($temp[1] % 64);
                    $x[] = '|' . $number;
                    $count = 1;
                    $temp = array();
                }
            }
        }

        $x[] = '<';
        $x[] = '/';
        $x[] = 'a';
        $x[] = '>';

        $x = array_reverse($x);

        $output = "<script type=\"text/javascript\">\n"
            . "\t//<![CDATA[\n"
            . "\tvar l=new Array();\n";

        for ($i = 0, $c = count($x); $i < $c; $i++) {
            $output .= "\tl[" . $i . "] = '" . $x[$i] . "';\n";
        }

        $output .= "\n\tfor (var i = l.length-1; i >= 0; i=i-1) {\n"
            . "\t\tif (l[i].substring(0, 1) === '|') document.write(\"&#\"+unescape(l[i].substring(1))+\";\");\n"
            . "\t\telse document.write(unescape(l[i]));\n"
            . "\t}\n"
            . "\t//]]>\n"
            . '</script>';

        return $output;
    }

}

如果您的字符串中有电子邮件链接,则使用函数 safe_mailto。如果您不需要它,您可以自由修改代码。

然后你可以像往常一样在你的 Laravel 代码的任何部分使用这样的辅助类(这里是刀片模板,但原理是一样的):

<p>{!! \App\Helpers\Helpers::auto_link($string) !!}</p>

又快又脏,而且有效。希望有所帮助。祝你好运!

于 2018-05-11T20:32:55.880 回答