How to remove non alphanumeric characters from a string in PHP

2 Answers

0 votes
$s = "@#$23PHP#(%*/?999~`;";

$s = preg_replace( '/[\W]/', '', $s);

echo $s;
      
 
/*
run:
 
23PHP999 
    
*/

 



answered Jan 29, 2019 by avibootz
0 votes
$s = "@#$23PHP#(%*/?999~`;";

$s = preg_replace( '/[^a-z0-9]/i', '', $s); 

echo $s;
      
 
/*
run:
 
23PHP999 
    
*/

 



answered Jan 29, 2019 by avibootz
...