Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace two different characters with one character using preg_replace() function?

How to replace @ and . symbols from an email address with - symbol using preg_replace() function in php ?

like image 391
francis mv Avatar asked Nov 03 '11 12:11

francis mv


2 Answers

There is no need to use preg_replace.

Use str_replace instead:

$output = str_replace(array('@', '.'), '-', $input);
like image 95
hsz Avatar answered Sep 29 '22 12:09

hsz


Since your search patterns are just strings, using string replacement using str_replace is better as suggested in other answer.

Here goes the preg_replace based answer:

$str = preg_replace('/@|\./','-',$str);
like image 23
codaddict Avatar answered Sep 29 '22 11:09

codaddict