PHP替换多个URL';在文本中使用锚定标记

PHP替换多个URL';在文本中使用锚定标记,php,regex,Php,Regex,到目前为止,我已经尝试了以下几点: <?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.o

到目前为止,我已经尝试了以下几点:

<?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;
}
试试这个:

// 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="$0">$0</a>', $text);

    echo $final;

} else {
    // if no urls in the text just return the text
    echo $text;
}
//正则表达式筛选器
$reg|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
$text=“我要筛选的文本在这里。它有URLhttp://www.example.com 及http://www.example.org";
//检查文本中是否有url
if(preg_match($reg_exUrl,$text,$url)){
//使URL成为超链接
$final=preg_replace($reg_exUrl,,$text);
echo$final;
}否则{
//如果文本中没有URL,请返回文本
echo$文本;
}
输出:

The text I want to filter is here. It has urls <a href="http://www.example.com">http://www.example.com</a> and <a href="http://www.example.org">http://www.example.org</a>
我要筛选的文本在这里。它有URL和
试试这个:

// 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="$0">$0</a>', $text);

    echo $final;

} else {
    // if no urls in the text just return the text
    echo $text;
}
//正则表达式筛选器
$reg|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
$text=“我要筛选的文本在这里。它有URLhttp://www.example.com 及http://www.example.org";
//检查文本中是否有url
if(preg_match($reg_exUrl,$text,$url)){
//使URL成为超链接
$final=preg_replace($reg_exUrl,,$text);
echo$final;
}否则{
//如果文本中没有URL,请返回文本
echo$文本;
}
输出:

The text I want to filter is here. It has urls <a href="http://www.example.com">http://www.example.com</a> and <a href="http://www.example.org">http://www.example.org</a>
我要筛选的文本在这里。它有URL和

只需使用一个
preg\u replace()

使用
preg_match()
没有意义,regex调用在性能方面相对昂贵


顺便说一句:我还对您的正则表达式进行了一些调整。

只需使用一个
preg\u replace()

使用
preg_match()
没有意义,regex调用在性能方面相对昂贵


附言:我还对你的正则表达式做了一些调整。

这正是我想要的!谢谢如果我想urlencode$0,那我该怎么办?这正是我要找的东西!谢谢如果我想urlencode$0,那我该怎么办?太好了。谢谢@HamZa@RipHunter请参见编辑。没有必要使用
preg\u match()
。回答得好。它回答了我所有的疑问。只有最后一个疑问@HamZa。如果我想对$0进行URL编码,那么我应该怎么做?这就是我想要的。谢谢,太好了。谢谢@HamZa@RipHunter请参见编辑。没有必要使用
preg\u match()
。回答得好。它回答了我所有的疑问。只有最后一个疑问@HamZa。如果我想对$0进行URL编码,那么我应该怎么做?这就是我想要的。谢谢