簡體   English   中英

解析字符串並將特殊 TEXT 鏈接 [a href=“url”]Text[/a] 轉換為 HTML 鏈接

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

如何解析字符串並將文本鏈接更改為 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 鏈接。

根據您向我們展示的確切輸入,您可以嘗試以下正則表達式替換:

$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>.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM