5

Possible Duplicate:
replace ereg_replace with preg_replace

I have got the following function within a code base that takes a String and makes links active. I have noticed that ereg_replace() is Depreciated. How would I change this to use preg_replace?

 function makeActiveLink($originalString){

        $newString = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=\"_blank\">\\0</a>", $originalString);
        return $newString;
    }
Community
  • 1
  • 1
Unleashed
  • 61
  • 4

2 Answers2

4

You can keep it almost exactly the same, but it would be preferable to change some things:

function makeActiveLink($originalString){
    $newString = preg_replace('#[a-z]+://[^<>\s]+[[a-z0-9]/]#i', '<a href="\0" target="_blank">\0</a>', $originalString);

    return $newString;
}

Note that I used # as a delimiter because you have slashes inside your string.

Ry-
  • 209,133
  • 54
  • 439
  • 449
1
function makeActiveLink($originalString) {
    $pattern '#[a-z]+://[^<>\s]+[[a-z0-9]/]#i';
    $newString = preg_replace($pattern, '<a href="\\0" target="_blank">\\0</a>', $originalString);

    return $newString;
}
PeeHaa
  • 69,318
  • 57
  • 185
  • 258