php remove numbers from string – How to remove all numbers from string?

php remove numbers from string Example A way to remove numbers is to replace them with an empty string. To remove numbers from string, we can use replace() method and simply replace.

php remove numbers from string

We will learn four different ways to remove numbers from a string. create an array populated with number 0-9 then use PHP str_replace function to do the search and replace.

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

Method 1

function deleteStrNo($userInput) {
    $num = array(0,1,2,3,4,5,6,7,8,9);
    return str_replace($num, null, $userInput);
}

echo deleteStrNo('5665Pakainfo7812'); 

Method 2

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

function deleteStrNo($userInput) {
   return preg_replace('/[0-9]+/', null, $userInput);
}

How to remove numbers from a String in PHP?


Don’t Miss : php remove non numeric

I hope you get an idea about php remove numbers from string.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment