PHP Remove Numbers From String

In this tutorial we will see How To Remove Numbers From String in PHP. PHP preg_replace() Function and PHP str_replace() Function can be used to remove all numbers from the given string.

PHP str_replace() Function

PHP str_replace() Function replaces characters of string with other pre-defined characters.

PHP str_replace() Function can be used to remove all numbers from the string.

PHP str_replace() Function can work with both arrays and strings.

Take a look at the example below.

<?php
$string = "123HowTo821Code720School789";
$array = array(0,1,2,3,4,5,6,7,8,9);
$result = str_replace($array,'', $string);
echo $result;
?>

Output

HowToCodeSchool

In above example we have used str_replace() to remove all numbers from the string.

In this example we have used pre-defined array with all numbers inside, the str_replace() will look for all numbers from array in the main string and will replace them with '' or null or nothing.

This will remove all numbers from string.

Let's see a better way to do this.

PHP preg_replace() Function

PHP preg_replace() Function also performs the search and replace function. It can be used to remove the numbers from sting more effectively.

PHP preg_replace() Function work with a pattern or regular expression. You can use preg_replace() function for both arrays and strings.

Take a look at the code below.

<?php
$string = "123HowTo821Code720School789";
$result = preg_replace('/[0-9]+/', '', $string);
echo $result;
?>

Output

HowToCodeSchool

In above example PHP preg_replace() Function is used to look for numbers and then remove them from the string. /[0-9]+/ is a regular expression or a pattern that we have used to find numbers.

'' is null which is our replacement for each number present in a string.

Take a look at another example.

<?php
$string = "123HowTo821Code720School789";
$result = preg_replace('/\d+/', '', $string);
echo $result;
?>

This is also one way to remove numbers from string in php. /\d+/ is a pattern which also looks for number.

How To Remove Empty Array Elements with PHP How To Remove Common Values from two Arrays with PHP How To Find sum of array values with PHP How To Shuffle array Values in PHP How to Remove Duplicate Values from an array with PHP PHP Sort Associative array by Key PHP Sort Associative array by Value Sort array in numerical order with PHP Sort array in alphabetical order with PHP Sort array in descending order PHP Sort array in ascending order in PHP How to merge two or more arrays in PHP Add elements to the end of an array in PHP Add elements to the start of an array in PHP PHP Remove First Element from an Array PHP Remove Last Element from an Array Check if Key Exists in array PHP Check if Value exists in array PHP How to reverse an array in PHP PHP Print array line by line PHP Echo array values using foreach loop Count array values in PHP PHP Count How Many Times Word appears in String How to Reverse a String with PHP Check if Variable is Null in PHP