-1

I've been searching around for this but all I could find was broken scripts and plus, I might have a method that is quite simple.

I'm trying to use a for () loop for this one.

This is what I've got:

<?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;
?>

However this fails brutally for some reason I can't comprehend.

The output from echo $makerepstring; as follows(from source code):

<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> 

I'd really like to do it with a for()... Could somebody try and figure out how to get this to work with me?

Thanks in advance!

/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;

This does not work either, although I'm not quite sure how you meant I should do this.

EDIT:

$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;
}

Would give:

<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>
Jafool
  • 5
  • 4
  • simplest solution is to just use http://www.php.net/manual/en/function.preg-replace.php instead. Don't use a for loop when the language has the full replacement functionality built in as a normal API function call. – Mike 'Pomax' Kamermans Dec 06 '13 at 22:44
  • @Mike'Pomax'Kamermans But I _am_ using preg_replace. I need to use it in a loop otherwise it won't work for several links in the same string of text. Right? – Jafool Dec 06 '13 at 22:47
  • no. The preg_replace function will replace all instances, unless your pattern is bad, or you explicitly give it a limit that isn't -1 – Mike 'Pomax' Kamermans Dec 06 '13 at 22:49
  • @Mike'Pomax'Kamermans Well, I've already tried this: `$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; $makeurl = $su_row['su_message']; if(preg_match($reg_exUrl, $makeurl, $url)) { // make the urls hyper links echo preg_replace($reg_exUrl, ''.$url[0].' ', $makeurl); } else { echo $makeurl; }` <-- That simply display the first URL written in the string TWICE. – Jafool Dec 06 '13 at 22:54
  • so update your post to show all the things you've tried so far, and what their result was. Don't add it in a comment, because no one can read code in comments – Mike 'Pomax' Kamermans Dec 06 '13 at 22:55
  • I'm a little confused, the output looks like what you wanted to get, what part of that output is not correct yet? – Mike 'Pomax' Kamermans Dec 06 '13 at 23:03
  • @Mike'Pomax'Kamermans Yeah I'm so sorry, I edited my post again it's correct now. I'm just a little tired, haha. Check out my first post again. :) – Jafool Dec 06 '13 at 23:04
  • you're replacing with `url[0]` instead of with the regex's capture group `\0`. If you're using preg_replace, you don't need preg_match: just run the replace and its output is the string with your regexp replaced (as many times as it could based on the input, search/replace pattern, and limit). Have `\0` in the replacement pattern and you should be good. – Mike 'Pomax' Kamermans Dec 06 '13 at 23:07
  • @Mike'Pomax'Kamermans Okay. I'm new to the whole preg_match and regex stuff. This is pretty much the first time I ever used it. So I'm sorry, but I don't know where I'd put \0 in the regex. Should I not use $url[0] in the preg_replace either? Could you explain it a little further to me, please? – Jafool Dec 06 '13 at 23:12
  • this post is not the place for that explanation, but http://www.regular-expressions.info and http://www.regular-expressions.info/examples.html are – Mike 'Pomax' Kamermans Dec 06 '13 at 23:14

2 Answers2

0

that's not how preg_match_all works. http://php.net/manual/en/function.preg-match-all.php shows you that the matches go in a passed-along array, and the function returns the number of matches, instead. So first call

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

And then iterate over the $matches array, which now has content.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Updated my original post, check it out. – Jafool Dec 06 '13 at 22:41
  • ... you updated it in a weird way, but: you would iterate over the $matches array because that contains the actual matches. At this point I'm going to point you at the php.net manual and say "read the function descriptions and the comments, which contain a wealth of examples" – Mike 'Pomax' Kamermans Dec 06 '13 at 22:45
0

You are performing the match twice:

  1. in preg_match_all function
  2. then you are matching again in preg_replace, which should not happen here

Use string concatation instead:

$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;
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56
  • Well, that doesn't work either because it totally leaves out all other text but the links. – Jafool Dec 06 '13 at 22:39
  • Thats because with this regex you won't be able to get the surrounding text. Take a look at [this anwser](http://stackoverflow.com/a/1188460/1697320). – Maciej Sz Dec 06 '13 at 22:49