Remove all non alphabetic chars from string in PHP
The regular expression ‘/[\W]/’ matches all the non-alphanumeric characters and replace them with ‘ ‘ (empty string).
<?php
// string containing non-alphanumeric characters
$str="!@GeeksforGeeks2018?";
// preg_replace function to remove the
// non-alphanumeric characters
$str = preg_replace( '/[\W]/', '', $str);
// print the string
echo($str);
?>
Output
GeeksforGeeks2018
The regular expression ‘/[^a-z0-9 ]/i’ matches all the non-alphanumeric characters and replace them with ‘ ‘ (null string).
<?php
// string containing non-alphanumeric characters
$str="!@GeeksforGeeks2018?";
// preg_replace function to remove the
// non-alphanumeric characters
$str = preg_replace( '/[^a-z0-9]/i', '', $str);
// print the string
echo($str);
?>
Output
GeeksforGeeks2018