【问题标题】:preg_replace to replace string for matching urlpreg_replace 替换字符串以匹配 url
【发布时间】:2013-07-25 10:06:56
【问题描述】:
function makeLinksInTheContent($html)
{
    $html= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" rel=\"nofollow\" >$3</a>", $html);
    $html= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" rel=\"nofollow\" >$3</a>", $html);
    $html= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\" rel=\"nofollow\">$2@$3</a>", $html);

    return($html);
}

这是我的代码。

我需要自动链接网址。使用 preg_replace 查找 url 并设置指向该 url 的链接。

例如:“一个页面包含 www.google.com。”如果我将此内容传递给 makeLinksInTheContent($html),它将返回“A page contains www.google.com.”

但以下网址格式未链接。

  1. (http://www.google.com)

  2. www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>

  3. http://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>

  4. https://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>

  5. ftp://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>

我认为我的正则表达式有一些错误。请建议我们。

【问题讨论】:

标签: php regex preg-replace


【解决方案1】:

在这种情况下,您可以使用preg_replace_callbackRead more

功能

<?php
  function replace_urls( $text = null ) {
    $regex  = '/((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/';
    return preg_replace_callback( $regex, function( $m ) {
      $link = $name = $m[0];
      if ( empty( $m[1] ) ) {
        $link = "http://".$link;
      }
      return '<a href="'.$link.'" target="_blank" rel="nofollow">'.$name.'</a>';
    }, $text );
  }
?>

用法

<?php
  $text = "http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054
  www.google.com
  https://twitter.com/
  http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar";

  echo replace_urls( $text );
?>

输出

<a href="http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054" target="_blank" rel="nofollow">http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054</a>
<a href="http://www.google.com" target="_blank" rel="nofollow">www.google.com</a>
<a href="https://twitter.com/" target="_blank" rel="nofollow">https://twitter.com/</a>
<a href="http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar" target="_blank" rel="nofollow">http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar</a>

【讨论】:

  • 以下网址未链接。
【解决方案2】:

我对你上次问这个的回答:

preg_replace to replace string for matching url

我可以建议使用我的功能 - 刚刚使用您的数据进行了测试并为我工作

function parse_links($string,$mailto=true)
{
    $preg_r = "#(((https?|ftp)(://)?)?[a-zA-Z0-9-_.]{1,64}\.[a-zA-Z0-9-_.]{1,128}\.[a-z]{2,4}(\.[a-z]{2,4})?(/([^ ]{1,256}))?/?)#";
    $string = preg_replace($preg_r,"<a href=\"http://$1\">$1</a>",explode(" ",$string));
    if($mailto)
    {
        $preg_r2 = "/([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z_\-\.][-\w]*[0-9a-zA-Z_\-\.]\.)+[a-zA-Z]{2,9})/";
        $string = preg_replace($preg_r2,"<a href=\"mailto:$1\">$1</a>",$string);
    }
    return implode(" ",$string);
}

如果您不想创建电子邮件链接,只需将 false 作为第二个参数传递

【讨论】:

  • 以下网址未链接。 (www.google.com), www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?>.co,in,com.com, https://www.test.com()and[]&^%$#@!+|!@#$%^&()_+}{:"?><,./;'[]=-09~.co,in,com.com
  • 当我测试字符串 www.google.com www.test.comhttps://www.test.com 时,我得到了三个链接 - 你还希望捕获什么??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 2014-08-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
相关资源
最近更新 更多