Jump to content

help converting eregi_replace to php 5 code


redbrad0

Recommended Posts

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);

Link to comment
Share on other sites

$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 

 

 

Link to comment
Share on other sites

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.