How to Remove Numbers from String in PHP?

In PHP, you can use regular expressions to remove numbers from a string. Here's an example:
$string = "This is a string with 123 numbers."; $pattern = '/\d+/'; // This pattern matches any sequence of digits $replacement = ''; $new_string = preg_replace($pattern, $replacement, $string); echo $new_string; // Output: "This is a string with numbers."
In this example, we first define the input string and then define a regular expression pattern that matches any sequence of digits (\d+). We then use the preg_replace() function to replace any matches of this pattern with an empty string, effectively removing them from the string. The resulting string ($new_string) is then echoed out.

For Western Arabic numbers (0-9):

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

For all numerals including Western Arabic (e.g. Indian):

$words = '१३३७';
$words = preg_replace('/\d+/u', '', $words);
var_dump($words); // string(0) ""


Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755