tags:

views:

380

answers:

2

I need help converting eregi_replace to preg_replace (since in PHP5 it's depreciated):

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

(It turns text links and emails into hyperlinks so that the user can click on them)

+3  A: 

Start with a look at the list of differences between POSIX and PCRE expressions in the manual.

If your expressions aren't complicated, generally it means that you can get away with simply putting delimiters around your $pattern parameter, and switch to use the preg family of functions. In your case, you would do this:

 function makeClickableLinks($text)
 {
 $text = preg_replace('/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
                       '<a href="\\1">\\1</a>', $text);
 $text = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
                       '\\1<a href="http://\\2"&gt;\\2&lt;/a&gt;', $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);
 return $text;
 }

Note the / characters around your patterns, and the i flag after the delimiters. I tested this quickly, and it worked for a basic URL. You'll probably want to test it more thoroughly.

zombat
Bet you a dollar he just copy/pastes this into his code and doesn't even try to understand. +1 for a good answer however.
Byron Whitlock
Thank you for the answer, I'll be looking into the link you have posted and will use your advice to convert other eregi_replace to preg_replace.
alexy13
Fantastic answer. Both for the conversion of the function (which is commonly used) and for the pointer to that link. I was looking through the php manual but didn't come across that page.
Gerry
A: 

How can I fix this one

    $str = eregi_replace("<[[:space:]]*([^>]*)[[:space:]]*>",'<\\1>', $str);
// Delete all spaces from html tags .
$str = eregi_replace("<a[^>]*href[[:space:]]*=[[:space:]]*\"?[[:space:]]*([^\" >]*)[[:space:]]*\"?[^>]*>",'<a href="\\1">', $str);
// Delete all attribs from Anchor, except an href, double quoted.
$str = eregi_replace("<[[:space:]]* img[[:space:]]*([^>]*)[[:space:]]*>", '', $str);
// Delete all img tags
$str = eregi_replace("<a[^>]*href[[:space:]]*=[[:space:]]*\"?javascript[[:punct:]]*\"?[^>]*>", '', $str);
// Delete javascript code from a href tags -- Zhen-Xjell @ http://nukecops.com
$tmp = "";
while (ereg("<(/?[[:alpha:]]*)[[:space:]]*([^>]*)>",$str,$reg)) {
Orlando
There is a few resources you can use to convert your eregi_replace to preg_replace, such as tutorial(s), but my answer doesn't explain how to complete your question: bit.ly/9Ax6s3 , http://bit.ly/9yrouz . You may also want to start a new question with your "answer".
alexy13
Replies are for answers not new questions.
Gerry