使用for循环替换带链接的URL

时间:2013-12-06 22:26:45

标签: php

我一直在寻找这个,但我能找到的只是破碎的脚本而且,我可能有一个非常简单的方法。

我正在尝试为此使用for()循环。

这就是我所拥有的:

<?php
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

$makerepstring = "Here is a link: http://youtube.com and another: http://google.com";

 if(preg_match_all($reg_exUrl, $makerepstring, $url)) {

     // make the url into link
     for($i=0; $i < count(array_keys($url[0])); $i++){
   $makerepstring = preg_replace($reg_exUrl, '<a href="'.$url[0][$i].'" target="_blank" rel="nofollow">'.$url[0][$i].'</a> ', $makerepstring);
     }

 }

 echo $makerepstring;
?>

然而,由于某种原因,我无法理解这种情况。但

echo $makerepstring;的输出如下(源代码):

<a href="<a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a> " target="_blank" rel="nofollow"><a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a> </a>  <a href="<a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a> " target="_blank" rel="nofollow"><a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a> </a> 

我真的很想用for()来做...有人可以试着弄清楚如何让它与我一起工作吗?

提前致谢!

/ J

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

$makerepstring = "http://youtube.com http://google.com";

$url = array();

$instances = preg_match_all($reg_exUrl, $makerepstring, $url);

if ($instances > 0) {

    // make the url into link
    for($i=0; $i < count(array_keys($url[0])); $i++){
        $makerepstring = preg_replace($reg_exUrl, '<a href="'.$url[0][$i].'" target="_blank" rel="nofollow">'.$url[0][$i].'</a> ', $makerepstring);
        /*echo $url[0][$i]."<br />";
        echo $i."<br />";
        print_r($url);
        echo "<br />";*/
    }

}

echo $makerepstring;

这也不起作用,虽然我不太清楚你的意思我应该这样做。

修改

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

$makeurl = "http://google.com http://youtube.com";

if(preg_match($reg_exUrl, $makeurl, $url)) {

       echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank" rel="nofollow">'.$url[0].'</a> ', $makeurl);

} else {
echo $makeurl;
}

会给:

<a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a> <a href="http://google.com" target="_blank" rel="nofollow">http://google.com</a>

2 个答案:

答案 0 :(得分:0)

这不是preg_match_all的工作方式。 http://php.net/manual/en/function.preg-match-all.php向您显示匹配在传递的数组中,并且函数返回匹配数。所以先打电话

...
$matches = array();
$instances = preg_match_all(..., $matches);
if ($instances > 0) {
  // and then your code
}
...

然后遍历$matches数组,该数组现在有内容。

答案 1 :(得分:0)

您正在进行两次比赛:

  1. in preg_match_all function
  2. 然后你再次在preg_replace中匹配,这不应该在这里发生
  3. 改为使用字符串连接:

    $makerepstring = "Here is a link: http://youtube.com and another: http://google.com";
    
    $new_str = '';
    
     if(preg_match_all($reg_exUrl, $makerepstring, $url)) {
    
         var_dump($url[0]);
    
         // make the url into link
         for($i=0; $i < count(array_keys($url[0])); $i++){
             $new_str .= '<a href="'.$url[0][$i].'" target="_blank" rel="nofollow">'.$url[0][$i].'</a> ';
         }
    
     }
    
     echo $new_str;