How to remove all numbers from string?

前端 未结 5 1802
孤街浪徒
孤街浪徒 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:00

    Use Predefined Character Ranges

    echo $words= preg_replace('/[[:digit:]]/','', $words);

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-12-02 17:14

    Regex

       $words = preg_replace('#[0-9 ]*#', '', $words);
    
    0 讨论(0)
  • 2020-12-02 17:16

    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.

    0 讨论(0)
  • 2020-12-02 17:20

    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.

    0 讨论(0)
提交回复
热议问题