How to Remove Numbers from String in PHP?

 

In this blog, we will be going to remove special characters from strings in PHP. This will be very easy and simple to remove special characters from a string. 

We will be going to use the function provided by PHP which will help to remove special characters that function is preg_replace(). 


Let's check the example,


Example 1:


Code:


<?php

 

$str = "Hello, This is 1234 thecodingdev.com";

$newString = preg_replace('/[0-9 ]+/', '', $str);


echo $newString;

   

?>


Output:


Hello, This is thecodingdev.com



Example 2:


Code:


<?php

 

   $string = "Hello, This is 1234 thecodingdev.com".

  

   $newString = preg_replace('/\d/', '', $string);

  

   print($newStrin);

 

?>


Output:


Hello, This is thecodingdev.com



I hope this code will help. If you have any doubt let me know in comments.