How to remove all numbers from string?

前端 未结 5 1801
孤街浪徒
孤街浪徒 2020-12-02 16:58

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         


        
5条回答
  •  遥遥无期
    2020-12-02 17:11

    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.
    • The modifier /u enables unicode string treatment. This modifier is important, otherwise the numerals would not match.

提交回复
热议问题