-1

I know there are lots of questions on that but i didn't found anything matches with my question. I want to convert this expression to preg_replace:

$a = ereg_replace('[-a-z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*','', $a);

so far tried those but didn't worked:

$a = ereg_replace('/[-a-z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*/','', $a);
$a = ereg_replace('|[-a-z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*|','', $a);

Here is the error message for second line:

Warning: preg_replace(): Unknown modifier '}' in
hakre
  • 193,403
  • 52
  • 435
  • 836
mirza
  • 5,685
  • 10
  • 43
  • 73
  • Try `preg_quote`ing the stuff in your character class. – Waleed Khan Feb 21 '13 at 22:50
  • In the second one, you are using the delimiter inside the pattern without quoting it (just before the `}`) – A. Rodas Feb 21 '13 at 22:51
  • 1
    Escape the delimiter. Or use a delimiter which doesn't occur in your character list, e.g. `°`. If all else fails `preg_match("\001[....]\001"` is always an alternative. – mario Feb 21 '13 at 22:52

1 Answers1

1

for the character that delimits the match string, use something that's not in the match string, or escaping the uses of the delimiter within the string, like

$a = ereg_replace('/[-a-z0-9!#$%&\'*+\/=?^_`{|}~]+@([.]?[a-zA-Z0-9_\/-])*/','', $a);

That particular example is kind of hard to read; I'd find a character that's not used at all.

Jerry
  • 3,391
  • 1
  • 19
  • 28