Jump to content

eregi_replace() to preg_replace code help


Go to solution Solved by 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 ------------+

Edited by Ch0cu3r

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

 

 

You are using a ~ as the pattern delimiter, therefore when using a ~ within your regex pattern you need to escape it, like how I showed in my   escape pattern delimiter   comment from my first post (a \ before the ~).

Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.