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

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

將所有 url 替換為 PHP 中的鏈接

將所有 url 替換為 PHP 中的鏈接

PHP
慕斯709654 2023-08-19 14:19:51
我想將文本中的所有網址設為超鏈接。我做了以下事情。但鏈接缺少“:”。任何幫助表示贊賞。<?php$string = "Hallo Studenten http://google.com/your/subpage and https://www.yahoo.com/my/subpage";  $regexp = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";  $anchorMarkup = "<a href=\"%s://%s\" target=\"_blank\" >%s</a>";  preg_match_all($regexp, $string, $matches, \PREG_SET_ORDER);  foreach ($matches as $match) {    if (empty($match[1]) && empty($match[7])) {      $http = $match[2]?$match[2]:'http';      $replace = sprintf($anchorMarkup, $http, $match[0], $match[0], $match[1]);      $string = str_replace($match[0], $replace, $string);    }  }  echo $string; ?>http://phpfiddle.org/lite/code/2yg5-i3uj
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

可以使用preg_replace函數來完成:

$string? ? ? ?= "Hallo Studenten http://google.com/your/subpage and https://www.yahoo.com/my/subpage";

$regexp? ? ? ?= "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";

$anchorMarkup = "<a href=\"$0\" target=\"_blank\" >$0</a>";


echo preg_replace($regexp, $anchorMarkup, $string);


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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