I\'d like to remove all numbers from a string [0-9]. I wrote this code that is working:
$words = preg_replace(\'/0/\', \'\', $words ); // remove numbers
$wor
Use Predefined Character Ranges
echo $words= preg_replace('/[[:digit:]]/','', $words);
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) ""
\d+
matches multiple numerals./u
enables unicode string treatment. This modifier is important, otherwise the numerals would not match.Regex
$words = preg_replace('#[0-9 ]*#', '', $words);
Use some regex like [0-9]
or \d
:
$words = preg_replace('/\d+/', '', $words );
You might want to read the preg_replace() documentation as this is directly shown there.
Try with regex \d
:
$words = preg_replace('/\d/', '', $words );
\d
is an equivalent for [0-9]
which is an equivalent for numbers range from 0
to 9
.