redbrad0 Posted April 17, 2011 Share Posted April 17, 2011 Since eregi_replace is deprecated I am trying to convert it to a new regexp replace but cant seem to get it. I thought you could just replace eregi_replace with preg_replace but that is not working. Can someone help me with converting this as I cant seem to figure it out. $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="' . $linkprefix . '\\1">\\1</a>', $text); $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="' . $linkprefix . 'http://\\2">\\2</a>', $text); $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text); Quote Link to comment https://forums.phpfreaks.com/topic/233930-help-converting-eregi_replace-to-php-5-code/ Share on other sites More sharing options...
.josh Posted April 17, 2011 Share Posted April 17, 2011 $text = preg_replace('~(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.\~#?&//=]+)~i', '<a href="' . $linkprefix . '$1">$1</a>', $text); $text = preg_replace('~([\s()\[\{\}])(www.[-a-zA-Z0-9@:%_\+.\~#?&//=]+)~i', '$1<a href="' . $linkprefix . 'http://$2">$2</a>', $text); $text = preg_replace('~([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})~i', '<a href="mailto:$1">$1</a>', $text); Basically the things to note for converting php's posix regex functions to pcre regex are: - The pcre functions need a starting/ending delimiter around the pattern, because pattern modifiers come after the ending delimiter. The delimiter can be pretty much any non-alphanumeric character, as long as you remember to escape the character you choose, if you need to use that character within the pattern. Example: "~pattern~modifier" - The posix functions (ereg) have two separate functions for making case-insensitive (ereg vs. eregi) whereas the pcre functions use the i modifier - posix regex has some "shortcut" character classes like :space: or :alnum: don't work, but do have pcre counterparts. Like \s is for :space: and \w is for :alnum: - The captured groups for replacements are represented by $n instead of \\n Quote Link to comment https://forums.phpfreaks.com/topic/233930-help-converting-eregi_replace-to-php-5-code/#findComment-1202454 Share on other sites More sharing options...
requinix Posted April 17, 2011 Share Posted April 17, 2011 - The captured groups for replacements are represented by $n instead of \\n \n works too. Quote Link to comment https://forums.phpfreaks.com/topic/233930-help-converting-eregi_replace-to-php-5-code/#findComment-1202539 Share on other sites More sharing options...
.josh Posted April 17, 2011 Share Posted April 17, 2011 - The captured groups for replacements are represented by $n instead of \\n \n works too. Yeah but it can get messy real quick so it's generally better to stick with $n Quote Link to comment https://forums.phpfreaks.com/topic/233930-help-converting-eregi_replace-to-php-5-code/#findComment-1202668 Share on other sites More sharing options...
redbrad0 Posted April 19, 2011 Author Share Posted April 19, 2011 Thanks everyone for all the details and examples. Quote Link to comment https://forums.phpfreaks.com/topic/233930-help-converting-eregi_replace-to-php-5-code/#findComment-1203755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.