【问题标题】:How to convert a link in a textarea into a link-element using php?如何使用 php 将 textarea 中的链接转换为链接元素?
【发布时间】:2011-06-01 16:32:19
【问题描述】:

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

例如:

Ask this on http://stackoverflow.com now

成为

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

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

谁能告诉我解决办法?

【问题讨论】:

  • 您需要处理提交的字符串以检查(正则表达式)它是一个有效的 url。之后您可以返回一个带有该字符串作为 href 属性的 标签 链接
  • 输入到文本框中的所有内容都是链接,还是混合数据?
  • 感谢 Frank Farmer,帮了大忙。

标签: php html string


【解决方案1】:

有几种解决方案可以解决这个问题,其中大多数都写得不好且不完整。在这种情况下,我建议使用其中一个大型框架的现有解决方案,无需重新发明轮子。

例如,this article on Zenverse 描述了 WordPress 处理此问题的方式。

让我在这里添加 sn-p 以供进一步参考:

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

链接文章中的示例用法:

$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);

【讨论】:

    【解决方案2】:

    您需要首先找到作为链接的子字符串。为此,您可以使用正则表达式。然后,您需要在链接周围添加 html 标签。 preg_replace 应该是你的朋友。

    例如(简化示例):

    $linkedtext = preg_replace ( '@\bhttp://([a-zA-Z0-9.%/]+)\b@', '<a href="http://$1">$1</a>', $text)
    

    要获得更好的 url 匹配正则表达式,请参阅Regex to match URL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2020-02-14
      • 2012-07-12
      • 2018-03-30
      • 1970-01-01
      • 2018-03-31
      相关资源
      最近更新 更多