PHP: Best way to check if input is a valid number?

前端 未结 6 763
清酒与你
清酒与你 2020-11-30 07:13

What is the best way of checking if input is numeric?

  • 1-
  • +111+
  • 5xf
  • 0xf

Those kind of numbers should not be valid. Onl

6条回答
  •  眼角桃花
    2020-11-30 07:33

    The most secure way

    if(preg_replace('/^(\-){0,1}[0-9]+(\.[0-9]+){0,1}/', '', $value) == ""){
      //if all made of numbers "-" or ".", then yes is number;
    }
    

提交回复
热议问题