我想将文本中的所有网址设为超链接。我做了以下事情。但链接缺少“:”。任何帮助表示赞赏。
<?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
慕姐8265434