yandex
loader

please wait

  • Kamal Thakur Mar-11-2019 01:21:24 PM ( 1 week ago )

    I have a PHP app that get's a play store app data and I want to check if the data I am getting is the version number.

    A version number can be 0.0.1 to 99.99.99.

    How can I properly use regular expressions to handle this job?

     

    This is how I currently do it:

    if (checkIfValidVersion($someString)) {
        echo "valid version!";
    }
    
    function checkIfValidVersion ($someString) {
        $split = explode(".", $someString);
        $isValid = false;
    
        foreach($split as $s) {
            if (is_numeric($s)) {
                $isValid = true;
            } else {
                $isValid = false;
            }
        }
    
        return $isValid;
    }
  • Dipti Singh Mar-11-2019 01:23:17 PM ( 1 week ago )

    Use:

    ^(\d?\d)\.(\d?\d)\.(\d?\d)$

    That regex will match three groups of 1 or 2 digit numbers separated by dots. Examples:

    preg_match("/^(\d?\d)\.(\d?\d)\.(\d?\d)$/", "1.0.0"); // Match
    preg_match("/^(\d?\d)\.(\d?\d)\.(\d?\d)$/", "99.99.99"); // Match
    preg_match("/^(\d?\d)\.(\d?\d)\.(\d?\d)$/", "99.699.99"); // Not match

Please login

Similar Discussion

Recommended For You