开发者

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

开发者 https://www.devze.com 2023-03-29 04:16 出处:网络
This question already has answers here开发者_C百科: Closed 10 years ago. Possible Duplicate: replace ereg_replace with preg_replace
This question already has answers here开发者_C百科: Closed 10 years ago.

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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号