PHP regular expression - filter number only

后端 未结 6 673
南旧
南旧 2020-11-30 04:46

I know this might sound as really dummy question, but I\'m trying to ensure that the provided string is of a number / decimal format to use it later on with PHP\'s number_fo

6条回答
  •  臣服心动
    2020-11-30 04:53

    To remove anything that is not a number:

    $output = preg_replace('/[^0-9]/', '', $input);
    

    Explanation:

    • [0-9] matches any number between 0 and 9 inclusively.
    • ^ negates a [] pattern.
    • So, [^0-9] matches anything that is not a number, and since we're using preg_replace, they will be replaced by nothing '' (second argument of preg_replace).

提交回复
热议问题