【问题标题】:REGEX for bbcode links + non-bbcode URLs用于 bbcode 链接 + 非 bbcode URL 的 REGEX
【发布时间】:2013-06-17 15:51:21
【问题描述】:

Tehre 似乎不是如何以最佳方式做到这一点的明确答案。

我有一些 bbcode 可能有 bbcode 格式的链接:

[url=http://thisisalink.com]链接[/url]

以及可能的复制/粘贴网址:

http://thisisalink.com

我想用可点击的链接替换这两个实例。我目前有以下内容:正在运行的正则表达式:

"/\[link=http:\/\/(.*?)\](.*?)\[\/link\]/is"
"/\[link=https:\/\/(.*?)\](.*?)\[\/link\]/is"
"/\[link=(.*?)\](.*?)\[\/link\]/is"

$URLRegex = '/(?:(?<!(\[\/link\]|\[\/link=))(\s|^))'; // No [url]-tag in front and is start of string, or has whitespace in front
$URLRegex.= '(';                                    // Start capturing URL
$URLRegex.= '(https?|ftps?|ircs?):\/\/';            // Protocol
$URLRegex.= '\S+';                                  // Any non-space character
$URLRegex.= ')';                                    // Stop capturing URL
$URLRegex.= '(?:(?<![[:punct:]])(\s|\.?$))/i';      // Doesn't end with punctuation and is end of string, or has whitespace after

似乎我无法让两者都工作。在这种情况下,最后一个正则表达式似乎取消了第一个正则表达式的链接。

当然,这已经在某处记录了将 bbcode 链接和粘贴的 URL 链接在一起而不会相互冲突的最佳方式。

【问题讨论】:

    标签: php regex bbcode


    【解决方案1】:

    您可以做的是使用以 bbcode 模式开头的替换,以避免替换 bbcode 标签内的链接,例如:

    $pattern = '~\[url\s*+=\s*+([^]\s]++)]([^[]++)\[/url]|((http://\S++))~i';
    $result = preg_replace($pattern, '<a href="$1$3">$2$4</a>', $string);
    

    请注意,我已经捕获了两次复制/粘贴的 url,以避免使用 preg_replace_callback 函数。

    我为复制/粘贴的 url 使用了简化的模式,但是您可以将其替换为您想要处理的 https、ftp、ftps...

    【讨论】:

      【解决方案2】:

      我最终选择了这个。然后我通过它做一个回调,它允许我在 php 中做一些特殊的代码来检查一些链接:

      # MATCH '?://www.link.com' and make it a bbcode link
      $URLRegex = '/(?:(?<!(\[\/link\]|\[\/link=))(\s|^))'; // No [url]-tag in front and is start of string, or has whitespace in front
      $URLRegex.= '(';                                    // Start capturing URL
      $URLRegex.= '(https?|ftps?|ircs?|http?|ftp?|irc?):\/\/';            // Protocol
      $URLRegex.= '\S+';                                  // Any non-space character
      $URLRegex.= ')';                                    // Stop capturing URL
      $URLRegex.= '(?:(?<![[:punct:]])(\s|\.?$))/i';
      $output = preg_replace($URLRegex, "$2[link=$3]$3[/link]$5", $output);
      
      # MATCH 'www.link.com' and make it a bbcode link
      $URLRegex2 = '/(?:(?<!(\[\/link\]|\[\/link=))(\s|^))'; // No [url]-tag in front and is start of string, or has whitespace in front
      $URLRegex2.= '(';                                    // Start capturing URL
      $URLRegex2.= 'www.';            // Protocol
      $URLRegex2.= '\S+';                                  // Any non-space character
      $URLRegex2.= ')';                                    // Stop capturing URL
      $URLRegex2.= '(?:(?<![[:punct:]])(\s|\.?$))/i';
      $output = preg_replace($URLRegex2, "$2[link=http://$3]$3[/link]$5", $output);
      
      
      # link up a [link=....]some words[/link]
      $output = preg_replace_callback(
          "/\[link=(.*?):\/\/(.*?)\](.*?)\[\/link\]/is", 
          Array($this,'bbcode_format_link1'),
          $output);
      

      【讨论】:

        猜你喜欢
        • 2012-04-01
        • 2023-03-18
        • 1970-01-01
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        相关资源
        最近更新 更多