Jump to content

eregi_replace() to preg_replace code help


Devenvj

Recommended Posts

Hello Guys,

 

Need a small help, as this is complex for me. With PHP 5.4, our server throws "Deprecated: Function eregi_replace() is deprecated" for a file and need you experts help to convert below 3 lines and replace it with preg_replace function. Can any one help with new lines of below code :

 

$body_html = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $body_html);

 
$body_html = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $body_html);
 
$body_html = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $body_html);
 
Thank you & Appreciate it.
Link to comment
https://forums.phpfreaks.com/topic/286988-eregi_replace-to-preg_replace-code-help/
Share on other sites

You need to change eregi_replale to preg_replace, then apply delimiters to the start and end of your regex pattern and lastly use the case insenstive ( letter i ) pattern modifier

 

Example for the first one

//                                  escape pattern delimiter   +           + case insensitve pattern modifier
//                                                             |           |  
$body_html = preg_replace('~(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.\~#?&//=]+)~i', '<a href="\\1">\\1</a>', $body_html);
//                         ^                                              ^
//                         |                                              |
//                         +-------------- pattern delimiters ------------+

Hey Ch0cu3r

 

I did the above changes in all 3 lines, Although the Deprecated error is gone, a new warning pops up, below :

 

Line 1 : $body_html = preg_replace('~(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)~i', '<a href="\\1">\\1</a>', $body_html);
Line 2 : $body_html = preg_replace('~([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)~i', '\\1<a href="http://\\2">\\2</a>' $body_html);
Line 3 : $body_html = preg_replace('~([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})~i', '<a href="mailto:\\1">\\1</a>', $body_html);

 

New  warning : "Warning: preg_replace(): Unknown modifier '#'"   (For line 1 & 2)

 

Kindly help further... Thx

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.