我正在用鏈接替換純文本,但我做錯了。我嘗試了該preg_replace()功能,但它似乎根本無法解決我的問題。$string = 'This is a message with multiple links: http://google.com http://twitter.com http://google.com/qwerty http://facebook.com http://google.com/ytrewq';preg_match_all('/(^|\s)((http(s)?\:\/\/)?[\w-]+(\.[\w-]+)+[^\s]*[^.,\s])/u', $string, $url);$links = $url[2];foreach($links as $link){ $final_string = str_replace($link, '<a href="'.$link.'">'.$link.'</a>', $string);}echo $final_string;請注意,三個鏈接來自同一個域http://google.com,因此在替換第一個鏈接時,它會在其他鏈接中這樣做。foreach我用于需要為每個鏈接執行的函數的循環(我不寫它,因為它現在不重要)。我希望能夠單獨處理所有鏈接,并且共享域的鏈接不會相互踩踏。我得到的輸出:This is a message with multiple links: <a href="http://google.com">http://google.com</a> <a href="http://twitter.com">http://twitter.com</a> <a href="http://google.com">http://google.com</a>/qwerty <a href="http://facebook.com">http://facebook.com</a> <a href="http://google.com">http://google.com</a>/ytrewq我希望的輸出:This is a message with multiple links: <a href="http://google.com">http://google.com</a> <a href="http://twitter.com">http://twitter.com</a> <a href="http://google.com/qwerty">http://google.com/qwerty</a> <a href="http://facebook.com">http://facebook.com</a> <a href="http://google.com/ytrewq">http://google.com/ytrewq</a>
- 2 回答
- 0 關注
- 188 瀏覽
添加回答
舉報
0/150
提交
取消