時被替換不希望的路我用正則表達式我用鏈接替代的URL /井號標籤一個array
使用preg_replace
:鏈接使用的preg_replace
$regs = array('!(\s|^)((https?://|www\.)+[a-z0-9_./?=;&#-]+)!i', '/#(\w+)/');
$subs = array(' <a href="$2" target="_blank">$2</a>', '<a href="/hashtag/$1" title="#$1">#$1</a>');
$output = preg_replace($regs, $subs, $content);
如果$content
有一個鏈接,爲前:https://www.google.com/
,它會正確替換;如果有一個主題標籤,接着一文中,爲前:#hello
更換過,但是,如果有一個主題標籤的鏈接,例如:https://www.google.com/#top
更換如下:
#top" target="_blank">https://www.google.com/#top
^^^^ ^^^^
,只有突出部分變成鏈接。
如何解決?
什麼是你期望的輸出? – anubhava
我的預期輸出如下:'https:// www.google.com /#top',但它只檢測#top(因此破壞html),並且沒有檢測到完全鏈接 – Igor