【问题标题】:Regex http/www to links正则表达式 http/www 到链接
【发布时间】:2010-10-06 02:30:38
【问题描述】:

使以 http 或 www 开头的文本可点击的正则表达式看起来如何?

我现在的bbcode:

函数 bbcode($text) { $text = htmlspecialchars($text); $text = nl2br($text); $查找 = 数组( "'\[b\](.*?)\[/b\]'是", "'\[i\](.*?)\[/i\]'i", "'\[url\](.*?)\[/url\]'i" ); $替换=数组( "\\1", "\\1", “\\1” ); $text = preg_replace($find, $replace, $text); 返回$文本; }

如您所见,我使用 [url]link[/url] 链接 ATM。

提前致谢。

附:替换数组中的 html 不会显示...

【问题讨论】:

    标签: php regex


    【解决方案1】:

    这是一个不错的简单方法:

    Find: (http://[^ ]+)
    Replace: <a href="\\1">\\1</a>
    
    Find: (www\.[a-zA-Z0-9\-]\.[^ ]+)
    Replace: <a href="\\1">\\1</a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多