Two methods of PHP regularization to verify whether a string is a number or not with common regularization

  • 2021-11-29 06:11:26
  • OfStack

php Regular Verification String Is Numeric

Method 1:

In php, it is very easy to verify whether a string is a number by using regular expressions. The most important thing is how to write regular expressions well and master the writing method of regular expressions. Here, the method of judging numbers by using regular expressions is listed.


<?
if($str)
{
    if(eregi("^[0-9]+$",$str))
    {
          $str=(int)$str;
    }
    else
    {
          echo " The retrieved data is not a valid number type, and the operation will stop !";
          exit();
    }
}
else
{
    echo " The data to be validated is empty, and the operation stops !";
    exit();
}
?>

Method 2:

It is suggested that key parameters must be filtered. Such as digital regular filtering


if(preg_match("/^\d*$/",$fgid))  echo(' It's a number ');
else  echo(' Not numbers ');

Or use a function


if(is_numeric($fgid)) echo(' It's a number ');
else echo(' Not numbers ');[/code] The difference between these two methods is that  is_numeric Decimals are also considered numbers, while preceding regulars treat decimal points as characters. 

Attached are some commonly used regular operations:

Verification number: ^ [0-9] * $ Verify the number of n bits: ^\ d {n} $ Verify at least n digits: ^\ d {n,} $ Verify the number of m-n bits: ^\ d {m, n} $ Verify zero and non-zero beginning numbers: ^ (0 [1-9] [0-9] *) $ Verify a positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})? $ Verify positive real numbers with 1-3 decimal places: ^ [0-9] + (. [0-9] {1, 3})? $ Validate non-zero positive integers: ^\ +? [1-9] [0-9] * $ Validate non-zero negative integers: ^\-[1-9] [0-9] * $ Validate a non-negative integer (positive integer + 0) ^\ d + $ Validate a non-positive integer (negative integer + 0) ^ ((-\ d +) (0 +)) $ Validate characters of length 3: ^. {3} $ Validate a 26-letter string: ^ [A-Za-z] + $ Validate a string of 26 uppercase English letters: ^ [A-Z] + $ Validate a string of 26 lowercase English letters: ^ [a-z] + $ Validate a string consisting of numbers and 26 English letters: ^ [A-Za-z0-9] + $ Validate a string of numbers, 26 letters, or underscores: ^\ w + $ Verify the user password: ^ [a-zA-Z]\ w {5, 17} $The correct format is: it starts with a letter, is between 6 and 18 in length, and can contain only characters, numbers and underscores. Verify that it contains ^% & Characters such as',; =? $\ ": [^% & ',; =? $\ x22] + Verify Chinese characters: ^ [\ u4e00-\ u9fa5], {0,} $ Verify the Email address: ^\ w + [-+.]\ w +) * @\ w + ([-.]\ w +) *\.\ w + ([-.]\ w +) * $ Verify InternetURL: ^ http://([\ w-] +\.) + [\ w-] + (/[\ w-./? % & =] *)? $; ^ [a-zA-z] +://(w + (-w +) *) (. (w + (-w +) *)) * (? S*)? $ Verification phone number: ^ (\ d3, 4\ d3, 4\ d {3, 4}-)? \ d {7, 8} $:-The correct format is: XXXX-XXXXXXX, XXXX-XXXXXXXX, XXX-XXXXXXX, XXX-XXXXXXXX, XXXXXXX, XXXXXXXX. Verify ID number (15 or 18 digits): ^\ d {15}\ d {} 18 $ Validate 12 months of 1 year: ^ (0? [1-9] 1 [0-2]) $Correct format: "01"-"09" and "1" "12" Verify 31 days of a month: ^ ((0? [1-9]) ((12) [0-9]) 3031) $Correct format: 01, 09 and 1, 31. Integer: ^-? \ d + $ Non-negative floating point numbers (positive floating point numbers + 0): ^\ d + (\.\ d +)? $ Positive floating point number ^ (([0-9] +\. [0-9] * [1-9] [0-9] *) ([0-9] * [1-9] [0-9] *\. [0-9] +) ([0-9] * [1-9] [0-9] *) $ Non-positive floating point numbers (negative floating point numbers + 0) ^ ((-\ d + (\.\ d +)? ) (0 + (\. 0 +)? )) $ Negative floating point number ^ (-(([0-9] +\. [0-9] * [1-9] [0-9] *) ([0-9] * [1-9] [0-9] *\. [0-9] +) ([0-9] * [1-9] [0-9] *)) $ Floating point number ^ (-? \ d +) (\.\ d +)?

Summarize


Related articles: