How to remove numbers from a string with RegEx

前端 未结 8 617
花落未央
花落未央 2020-12-06 04:56

I have a string like this:

\" 23 PM\"

I would like to remove 23 so I\'m left with PM or (with space truncated) ju

相关标签:
8条回答
  • 2020-12-06 05:32

    If you just want the last two characters of the string, use substr with a negative start:

    $pm = substr("  23 PM", -2); // -> "PM"
    
    0 讨论(0)
  • 2020-12-06 05:34
    $str = preg_replace("/^[0-9 ]+/", "", $str);
    
    0 讨论(0)
  • 2020-12-06 05:40

    Can do with ltrim

    ltrim(' 23 PM', ' 0123456789');
    

    This would remove any number and spaces from the left side of the string. If you need it for both sides, you can use trim. If you need it for just the right side, you can use rtrim.

    0 讨论(0)
  • 2020-12-06 05:43

    You can also use the following:

    preg_replace('/\d/', '',' 23 PM' );
    
    0 讨论(0)
  • 2020-12-06 05:48

    Regex

    preg_replace('#[0-9 ]*#', '', $string);
    
    0 讨论(0)
  • 2020-12-06 05:50
    preg_replace("/[0-9]/", "", $string);
    
    0 讨论(0)
提交回复
热议问题