亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用 PHP 中相同域的鏈接替換純文本

用 PHP 中相同域的鏈接替換純文本

PHP
鳳凰求蠱 2021-07-06 13:50:56
我正在用鏈接替換純文本,但我做錯了。我嘗試了該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 回答

  • 2 回答
  • 0 關注
  • 188 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號