Home PHP How to remove all characters exept numbers from a string using PHP?
 
 
 

How to remove all characters exept numbers from a string using PHP?

PDF Print E-mail

Question: How to remove all characters exept numbers from a string using PHP?

 

 

Answer:

This is really easy, we will make use of the preg_replace PHP Function which is 100% compatible with PHP 5.3

 

 

Example:

 

 
 
$string = 'This is my phone number: +28 (0)212 1234!!! '
 
$numberString = preg_replace('[\D]', '', $string);
 
echo $numberString; //Will output 2802121234
 
 

 

This is a good way to format phone numbers etc.

 

The regular expression character \D means to match a character that's not a digit

 

 

+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!