Page 1 of 1

Replacing deprecated eregi

Posted: Mon May 19, 2014 10:27 pm
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?

Re: Replacing deprecated eregi

Posted: Tue May 20, 2014 11:36 am
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);

Re: Replacing deprecated eregi

Posted: Tue May 20, 2014 6:40 pm
by barb woolums
Thanks