我已經試過到目前爲止以下:PHP替換多個URL的文本與錨標記
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "The text I want to filter is here. It has urls http://www.example.com and http://www.example.org";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
$final = preg_replace($reg_exUrl, "<a href=\"{$url[0]}\">{$url[0]}</a> ", $text);
echo $final;
} else {
// if no urls in the text just return the text
echo $text;
}
我現在面臨的唯一問題是,這是用相同的URL替換URL都的(也就是一個發現第一)。我如何loop
這個用自己替換每個url?
太棒了。謝謝@HamZa –
@RipHunter請參閱編輯。沒有必要使用'preg_match()'。 – HamZa
尼斯答案。它解答了我所有的疑問。 –