Replacing deprecated eregi

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Replacing deprecated eregi

Post by barb woolums »

I have the following code in a bbcode parser

Code: Select all

if (substr($Text,0, 7) == "http://"){
    $Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>", $Text);
Since eregi is now deprecated I which to convert it to preg_replace

Can I simply replace eregi_replace or is the regex different?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Replacing deprecated eregi

Post by Christopher »

It is simple to change to preg. Just change the function name to preg_replace() and add bounding slashes (and escape /'s or use another bound character) and add '/i' if you want it case insensitive. So yours would become:

Code: Select all

if (substr($Text,0, 7) == "http://"){
    $Text = preg_replace("/([[:alnum:]]+):\/\/([^[:space:]]*)([[:alnum:]#?/&=])/i", "<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>", $Text);
(#10850)
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Replacing deprecated eregi

Post by barb woolums »

Thanks
Post Reply