【问题标题】:Parse string and convert special TEXT link [a href="url"]Text[/a] to HTML link解析字符串并将特殊的 TEXT 链接 [a href="url"]Text[/a] 转换为 HTML 链接
【发布时间】:2021-03-19 12:33:45
【问题描述】:

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

例如:

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

收件人:

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

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

【问题讨论】:

    标签: php string parsing hyperlink


    【解决方案1】:

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

    $input = "Hello world: [a href=\"https://example.com\"]See example[/a].";
    $output = preg_replace("/\[(\/a|a href=\".*?\")\]/", "<$1>", $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>.
    

    【讨论】:

    • 但我必须检查 a 字符以确保它是带括号的链接文本。
    • 检查更新的答案,它只替换锚标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2017-04-14
    • 2011-10-27
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多