Replace numbers, dash, dot or space from the start of a string with PHP regex

前端 未结 2 1388
天涯浪人
天涯浪人 2021-01-25 18:34

I\'m trying to replace numbers with optional, dash, dot or space from the start of a string, my pattern does not seem to work. I want to replace this:

01. PHP
0         


        
2条回答
  •  独厮守ぢ
    2021-01-25 18:37

    Can try this

    $t = "01. PHP";
    
    $pattern = '/^[0-9\.\s\-]+/';
    
    echo preg_replace($pattern, '', $t);
    

    Output

    PHP
    

    Regex Explanation

    ^ assert position at start of the string
    0-9 a single character in the range between 0 and 9
    \. matches the character . literally
    \s match any white space character [\r\n\t\f ]
    \- matches the character - literally
    

提交回复
热议问题