Problem with preg_replace

Hello There,
I wrote a script and it will get data through $GET and after get i will change some character. Suppose my get data is some@website.com so i want to replace ‘@’ with '’ and i want to replace '. (dot) ’ with ‘-’ but i cant. Because preg_replace function allow me to put replace character only one. Here is my code


  $patterns = array();
  $patterns[0] = '/@/';
  $patterns[1] = '/\\./';
  $replacements ='-';
  $sType = preg_replace($patterns, $replacements, $searChtype);


my input is : some@website.com and output will be like this : some_website-com

If you know the way please share.
Thank you.

Give this a go.

<?php 
$searChtype = "some@website.com";
  
$patterns = array();
$patterns[0] = '/\\@/';
$patterns[1] = '/\\./';
$replacements = array();
$replacements[0] = '_';
$replacements[1] = '-';
$sType = preg_replace($patterns, $replacements, $searChtype);
echo  $sType;	
?>
some_website-com

preg_replace is kind of overkill here. Like killing a fly with a nuke. [fphp]str_replace[/fphp] will do just fine :slight_smile:


<?php
$email = str_replace(array('@', '.'), array('_', '-'), 'some@website.com'); // replace @ with _ and . with -
echo $email; // some_website-com

If you have to use preg_replace for some reason, @Drummin; 's solution is the way to go.

I would use str_replace, but wanted to fix OP’s code.