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;
}
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;
}
精彩评论