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
Contents
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?
<?php function cleanup_numbers($userInput) { $numbers = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " "); $userInput = str_replace($numbers, '', $userInput); return $userInput; } $member_name='pakainfo895656'; // will print jamrod echo cleanup_numbers($member_name); ?>
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.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.