Articles

How to remove all the special chracters from a string ?

To remove all the special character from a string

For example

Lets strip all the characters from telephone number

<?php
$tel = +91 9876-453-210
$tel = preg_replace('/[^a-zA-Z0-9]/','',$tel);
echo $tel;
?>

it will remove all the special characters from the string including hyphens, spaces, dots parenthesis etc.

String Output
preg_replace Expression : /[^a-zA-Z0-9]/
+91 9875.654.321 91987654321
+91 9875-654-321 91987654321
+91 9875.654.IND 919875654IND
+91 9875.654.Ind 919875654Ind
+91 (9875) 654-321 919875654321

Leave a comment