我已经试过到目前为止以下: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
尼斯答案。它解答了我所有的疑问。 –