解析字符串并将特殊的 TEXT link [a href="url"]Text[/a] 转换为 HTML link

Parse string and convert special TEXT link [a href="url"]Text[/a] to HTML link

如何解析字符串并将文本 link 更改为 HTML link。

例如:

Hello world: [a href="https://example.com"]See example[/a].

收件人:

Hello world: <a href="https://example.com">See example</a>.

我还必须确定目标 URL,如果指定了域,我必须将选项 target="_blank" 添加到 HTML link.

根据您向我们展示的确切输入,您可以尝试以下正则表达式替换:

$input = "Hello world: [a href=\"https://example.com\"]See example[/a].";
$output = preg_replace("/\[(\/a|a href=\".*?\")\]/", "<>", $input);
echo $input . "\n" . $output;

这会打印:

Hello world: [a href="https://example.com"]See example[/a].
Hello world: <a href="https://example.com">See example</a>.