Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing function to use preg_replace() instead of ereg_replace [duplicate]

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;
    }
like image 634
Unleashed Avatar asked Aug 20 '11 00:08

Unleashed


2 Answers

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.

like image 64
Ry- Avatar answered Sep 22 '22 14:09

Ry-


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;
}
like image 32
PeeHaa Avatar answered Sep 25 '22 14:09

PeeHaa