How to Remove Numbers from String in PHP?

03-Apr-2023

.

Admin

How to Remove Numbers from String in PHP?

Hi Dev,

This tutorial shows you how to remove numbers from string in php?. you can understand a concept of remove numbers from string in php example. Here you will learn remove number from string php. Here you will learn php in remove numbers from string.

There ara three ways to remove numbers from string in php. in the first example, we will use to preg_replace() function and this function use two different method. and the third example is created in using the function method.

Example 1:


index.php

<?php

$str = "123821Nice720nippets789.com";

// Remove All Numbers from String

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

echo $result;

?>

Output:

Nicenippets.com

Example 2:

index.php

<?php

$str = "76541Nice720nippets123.com";

// Remove All Numbers from String

$result = preg_replace('/\d/', '', $str );

echo $result;

?>

Output:

Nicenippets.com

Example 3:

index.php

<?php

// Create removeNumbers() Function

function removeNumbers($string) {

$num = array(0,1,2,3,4,5,6,7,8,9);

return str_replace($num, null, $string);

}

// Print the result

echo removeNumbers('123821Nice720nippets789.com');

echo removeNumbers('123821Remove720Numbers789demo');

echo removeNumbers('123821jaydeep720@7gmail89.com');

?>

Output:

Nicenippets.com

RemoveNumbersdemo

jaydeep@gmail.com

I hope it could help you...

#PHP