如何将textarea中的链接转换为使用php的链接元素? [英] How to convert a link in a textarea into a link-element using php?

查看:97
本文介绍了如何将textarea中的链接转换为使用php的链接元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个脚本,它包含一个发布脚本,但我希望用户直接从其他任何地方复制链接,当他们发布链接文本时,应自动将链接转换为链接元素( < a> )。



例如:

 在http://stackoverflow.com上问这个

变成

 在< a href =http:// stackoverflow.com> HTTP://stackoverflow.com< / A>现在

我试过了 str_replace()功能,但它不是解决方案。



任何人都可以告诉我一个解决方案吗? 方案

这里有几种解决方案,其中大部分都写得不好,不完整。在这种情况下,我建议使用现有的一个大框架解决方案,不需要重新发明轮子。



例如,这篇关于Zenverse的文章描述了 WordPress 处理这个问题。



让我在这里添加片段以供进一步参考:

 函数_make_url_clickable_cb($ matches){
$ ret ='';
$ url = $ matches [2];

if(empty($ url))
return $ matches [0];
//从URL
中删除尾部[。,;:] if(in_array(substr($ url,-1),array('。',',',';',':') ))=== true){
$ ret = substr($ url,-1);
$ url = substr($ url,0,strlen($ url)-1);
}
返回$ matches [1]。 < a href = \$ url \rel = \nofollow \> $ url< / a> 。 $ RET;
}

函数_make_web_ftp_clickable_cb($ matches){
$ ret ='';
$ dest = $匹配[2];
$ dest ='http://'。 $ DEST;

if(empty($ dest))
return $ matches [0];
//从URL
中删除尾部[,;:] if(in_array(substr($ dest,-1),array('。',',',';',':') )=== true){
$ ret = substr($ dest,-1);
$ dest = substr($ dest,0,strlen($ dest)-1);
}
返回$ matches [1]。 < a href = \$ dest \rel = \nofollow \> $ dest< / a> 。 $ RET;
}

函数_make_email_clickable_cb($匹配){
$ email = $ matches [2]。 '@'。 $匹配[3];
返回$ matches [1]。 < a href = \mailto:$ email \> $ email< / a>;
}

函数make_clickable($ ret){
$ ret =''。 $ RET;
//在测试中,在这里使用数组发现速度更快
$ ret = preg_replace_callback('#([\s>])([\w] +?:// [\ w \\x80-\\xff\#$%&〜/ .\ - ;:= =,?@ \ [\] +] *)#是','_make_url_clickable_cb',$ RET);
$ ret = preg_replace_callback('#([\s>])((www | ftp)\。[\w\\x80-\\xff\#$%& 〜/ .\ - ;; = =,?@ \ [\] +] *)#是','_make_web_ftp_clickable_cb',$ ret);
$ ret = preg_replace_callback('#([\s>])([。0-9a-z _ + - ] +)@(([0-9a-z-] + \。)+ 0-9a-z] {2,})#i','_make_email_clickable_cb',$ ret);

//这个不在数组中,因为我们需要它最后一次运行,以清理链接中的偶然链接
$ ret = preg_replace(#(< a([^ >] +> ||>))< a [^> +>([^>] +?)< / a>< / a> #i,$ 1 $ 3 < / a>,$ ret);
$ ret = trim($ ret);
返回$ ret;
}

链接文章中使用的示例用法:

  $ string ='我在这里有一些文字,还有如http://www.youtube.com,www.haha.com和lol @ example.com。他们准备好被替换。'; 

echo make_clickable($ string);


I was creating a script and it includes a posting script but I want users to directly copy a link from anywhere else and when they post it the link text should automatically convert the link to link-element (<a>).

So for example this:

Ask this on http://stackoverflow.com now

to become

Ask this on <a href="http://stackoverflow.com">http://stackoverflow.com</a> now

I have tried the str_replace() function but it wasn't the solution.

Can anyone tell me a solution to this?

解决方案

There are several solutions out there for this, most of them being poorly written and incomplete. In this situation I would advise to use an already existing solution of one of the big frameworks, there is no need to reinvent the wheel.

For example, this article on Zenverse describes the way WordPress handles this.

Let me add the snippet here for further reference:

function _make_url_clickable_cb($matches) {
    $ret = '';
    $url = $matches[2];

    if ( empty($url) )
        return $matches[0];
    // removed trailing [.,;:] from URL
    if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
        $ret = substr($url, -1);
        $url = substr($url, 0, strlen($url)-1);
    }
    return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $ret;
}

function _make_web_ftp_clickable_cb($matches) {
    $ret = '';
    $dest = $matches[2];
    $dest = 'http://' . $dest;

    if ( empty($dest) )
        return $matches[0];
    // removed trailing [,;:] from URL
    if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {
        $ret = substr($dest, -1);
        $dest = substr($dest, 0, strlen($dest)-1);
    }
    return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
}

function _make_email_clickable_cb($matches) {
    $email = $matches[2] . '@' . $matches[3];
    return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}

function make_clickable($ret) {
    $ret = ' ' . $ret;
    // in testing, using arrays here was found to be faster
    $ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
    $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
    $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);

    // this one is not in an array because we need it to run last, for cleanup of accidental links within links
    $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
    $ret = trim($ret);
    return $ret;
}

An example usage as written in the linked article:

$string = 'I have some texts here and also links such as http://www.youtube.com , www.haha.com and lol@example.com. They are ready to be replaced.';

echo make_clickable($string);

这篇关于如何将textarea中的链接转换为使用php的链接元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆