PHP regular expression - filter number only

后端 未结 6 665
南旧
南旧 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:57

    Using is_numeric or intval is likely the best way to validate a number here, but to answer your question you could try using preg_replace instead. This example removes all non-numeric characters:

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

提交回复
热议问题