如何使用 bbcode 标签的值将 bbcode url 标签转换为 html 超链接?

0 php regex bbcode hyperlink preg-replace

如何将 bbcode[url]标签转换为<a>在开始和结束标签之间具有 href 属性和文本的标签?

以下是一些示例字符串:

[url]https://any.com/any[/url]

[URL="https://any.com/any?any=333"]text text[/URL]

[url]http://www.any.com/any?any=44#sss[/url]

*请注意,开始[url]标记中的双引号子字符串是可选的,并且会影响所需的输出...

我试过这种模式:

(?:\[url="(https?://(?:www)?.+?)\]|\[url\](https?://(?:www)?.+\[)) 
\[url="(https?:\/\/(?:www\.)?.+?)\]|\[url\](https?:\/\/(?:www\.)?.+)\[\/url\]
\[url="(https?:\/\/(?:www\.)?.+)"\]|\[url\](https?:\/\/(?:www\.)?.+)\[\/url\]
Run Code Online (Sandbox Code Playgroud)

像这样:

$pattern ='##i';
$text = preg_replace($pattern,'',$text);
Run Code Online (Sandbox Code Playgroud)

从上面的 bbcode url 标签中,我想要的结果应该是:

<a href="https://any.com/any">https://any.com/any</a>

<a href="https://any.com/any?any=333">text text</a>

<a href="http://www.any.com/any?any=44#sss">http://www.any.com/any?any=44#sss</a>

换句话说,如果 url 位于开始[url]标记的双引号部分,则使用该值作为href值并将[url]标记的 innerHTML保留为生成的<a>标记的 innerHTML 。

如果URL不在双引号部分,但位于打开和关闭之间的[url]标签,然后使用该值作为这两个href值和的的innerHTML <a>

mic*_*usa 5

更新:卡西米尔评论的解决方案更直接/干净。

代码:(演示)(模式演示

echo preg_replace('~\[url(?|]((https?://[^[]+))|(?:="(https?://[^"]+)")](.+?))\[/url]~i', '<a href=\"$1\">$2</a>', $bbcode);
Run Code Online (Sandbox Code Playgroud)

通过将模式中第一个替代项的捕获加倍,您可以确保始终有一个$1and$2应用于替换字符串。

这是考虑单引号和不引用的模式的稍微扩展的变体


(上一个解决方案的开始)

通过使用,preg_match_callback()您可以确定是否在开始[url]标记内提供了 url —— 在这种情况下,您将希望保留位于开始标记和结束标记之间的文本。

如果标签之间的文本是 url,则在<a>标签字符串的两个位置都使用它。

无效的字符串将不会被转换。

代码:(演示)(模式演示

$bbcodes = [
    '[URL]www.no.http.example.com[/URL]',
    '[url]https://any.com/any[/url]',
    '[url="nourl"]nourl[/url]',
    '[URL="https://any.com/any?any=333"]text text[/URL]',
    '[url="http://www.emptyTEXT.com"][/url]',
    '[url]http://www.any.com/any?any=44#sss[/url]',
    '[url="https://conflictinglink"]http://differenturl[/url]'
];

foreach ($bbcodes as $bbcode) {
    echo preg_replace_callback('~\[url(?:](https?://[^[]+)|(?:="(https?://[^"]+)")](.+?))\[/url]~i',
                          function($m) {
                              if (isset($m[2])) {
                                  return "<a href=\"{$m[2]}\">{$m[3]}</a>";
                              }
                              return "<a href=\"{$m[1]}\">{$m[1]}</a>";
                          },
                          $bbcode);
    echo "\n---\n";
}
Run Code Online (Sandbox Code Playgroud)

输出:

[URL]www.no.http.example.com[/URL]
---
<a href="https://any.com/any">https://any.com/any</a>
---
[url="nourl"]nourl[/url]
---
<a href="https://any.com/any?any=333">text text</a>
---
[url="http://www.emptyTEXT.com"][/url]
---
<a href="http://www.any.com/any?any=44#sss">http://www.any.com/any?any=44#sss</a>
---
<a href="https://conflictinglink">http://differenturl</a>
---
Run Code Online (Sandbox Code Playgroud)

模式分解:

~                    #start of pattern delimiter
\[url                #match literally [url
(?:                  #start non-capturing group #1
  ]                  #match literally ]
  (https?://[^[]+)   #match and store as Capture Group #1 http , an optional s , colon , two forward slashes, then one or more non-opening square brackets (since valid href values cannot have square brackets)
  |                  #or
  (?:                #start non-capturing group #2
    ="               #match literally ="
    (https?://[^"]+) #match and store as Capture Group #2 (same logic as Capture Group #1)
    "                #match literally "
  )                  #end non-capturing group #2
  ]                  #match literally ]
  (.+?)              #match (lazily) and store as Capture Group #3 one or more characters (this is the innerHTML component)
)                    #end non-capturing group #1
\[/url]              #match literally [/url]
~                    #end of pattern delimiter
Run Code Online (Sandbox Code Playgroud)

回调函数评估匹配数组 ( $m) 中的元素,并有条件地生成并返回所需的输出。如果有任何匹配项,输出将包含:

array(
    0 => [the fullstring match]
    1 => [the url of a bbcode tag that does not have a quoted url]
)
Run Code Online (Sandbox Code Playgroud)

或者

array(
    0 => [the fullstring match]
    1 => ''  // <-- empty string
    2 => [the quoted url of the bbcode tag]
    3 => [the text between the opening an closing bbcode tags]
)
Run Code Online (Sandbox Code Playgroud)