Php preg_replace替换匹配url的字符串 函数makeLinksInTheContent($html) { $html=preg\u replace(“/(^ |[\n])([\w]*?)((ht | f)tp(s)?:\/\/[\w]+[^\,\”\n\r\t

Php preg_replace替换匹配url的字符串 函数makeLinksInTheContent($html) { $html=preg\u replace(“/(^ |[\n])([\w]*?)((ht | f)tp(s)?:\/\/[\w]+[^\,\”\n\r\t,php,regex,preg-replace,Php,Regex,Preg Replace,我对您上次提问的回答: 我可以建议使用我的功能-刚刚用你的数据测试过,对我有用 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=

我对您上次提问的回答:

我可以建议使用我的功能-刚刚用你的数据测试过,对我有用

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);
}
函数解析链接($string,$mailto=true) { $preg_r=“#”((https?| ftp)(:/)?)?[a-zA-Z0-9-{1,64}.[a-zA-Z0-9-{1128}.[a-z]{2,4}(\[a-z]{2,4})?(/([^]{1256}])?/”; $string=preg_replace($preg_r,“”,explode(“,$string)); 如果($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,“,$string); } 返回内爆(“,$string); }
如果您不想创建电子邮件链接,只需将false作为第二个参数传递

在这种情况下,您可以使用
preg\u replace\u callback

功能

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

用法

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


我以前已经回答过这个问题…?这个问题与以下URL没有链接的问题完全相同。以下URL没有链接。(www.google.com)、www.test.com()和[]和^%$$\@!+|!@$%^&(^+}{):“?>当我测试字符串
www.google.com
www.test.com
https://www.test.com
-您还希望捕获什么??
<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>