0

Hello i've writen a few scripts 2 or 3 years ago. Now it's not running. My scripts :

<?php
 function tolink($text){

    $text = " ".$text;
    $text = ereg_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);
    $text = ereg_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target="_blank" rel="nofollow">\\1</a>', $text);
    $text = ereg_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
    '\\1<a href="http://\\2" target="_blank" rel="nofollow">\\2</a>', $text);
    $text = ereg_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4})',
    '<a href="mailto:\\1"  rel="nofollow">\\1</a>', $text);
    return $text;
    }

    ?>

When i replace ereg_replace with preg_replace it gives me an error.

I need your help... Thank you...

  • Possible duplicate of [How can I convert ereg expressions to preg in PHP?](https://stackoverflow.com/questions/6270004/how-can-i-convert-ereg-expressions-to-preg-in-php) – Toto Jun 03 '19 at 10:29

1 Answers1

0

All the PCRE functions in PHP require that you surround the regexp with a pair of delimiters, e.g. the / surrounding the regexp in this example:

preg_replace ('/regexp_to_match/', 'replacement', $text);

This allows you to append modifiers to the regexp after the second delimiter, as in:

preg_replace ('#regexp_to_match#i', 'replacement', $text);

which performs a case-insensitive match.

As you can see from those two examples, the delimiters can be any character. The first character of the regexp is taken as the delimiter, and it then looks for its match at the end. If the character is a bracketing character, it looks for its opposite, e.g.

preg_match ('<x*>', $text);

Here's the preg_replace() version of your first substitution.

  $text = preg_replace('<(((f|ht){1}tp://)[-\w@:%+.~#?&/=]+)>',
        '<a href="$1" target="_blank" rel="nofollow">$1</a>', $text);

I've also simplified the regexp: I used \w for word characters instead of listing the alphanumerics explicitly, removed the unnecessary \ before + inside the [...], and changed // to / inside the brackets (the extra slash was redundant). Also, $1 is preferred these days in the replacement, rather than \\1.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • i've tried $text = preg_replace('/(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)/', '\\1', $text); – Eren Altun Aug 25 '13 at 23:49
  • You need to escape all `/` characters in the regular expression, since you're using it as the delimiter. Or use a different character as the delimiter, one that isn't in the regexp. – Barmar Aug 26 '13 at 00:41
  • I have all tried but it does not work... Can you give me an example please ? – Eren Altun Aug 26 '13 at 10:56