wobsoriano
wobsoriano

Reputation: 13434

How can I properly check a string format if it follows a version number format?

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 or I may be wrong with the version number limitations.

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

This is how I currently do it:

$list = ["hello", "0.23.2", "world"];

foreach($list as $l) {
    if (checkIfValidVersion($l)) {
       echo "valid version!";
    }
}

function checkIfValidVersion ($someString) {
    $split = explode(".", $someString);
    $isValid = false;

    foreach($split as $s) {
        $isValid = is_numeric($s) ? true : false;
    }

    return $isValid;
}

Upvotes: 2

Views: 939

Answers (6)

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

Use:

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

That regex will match three groups of 1 or 2 digit numbers separated by dots. It is good to use ^ and $ with preg_match to make sure it will match the whole string and not a string containing the version part, e.g. something1.0.0something.

Examples:

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

Upvotes: 4

Andreas
Andreas

Reputation: 23958

If you just explode the dot (.) then each item is limited to a maximum length of 2.

$version = "25.22.01";
$bool = true;
foreach(explode(".", $version) as $part) {
  if(strlen($part) > 2) {
    $bool = false;
  }
}

// Expected result: "true"
var_dump($bool);

Here is a working example at 3v4l.org.

Upvotes: 1

Malekai
Malekai

Reputation: 5011

Improving on shyammakwana.me's answer, I'd suggest using:

\d{1,2}\.\d{1,2}\.\d{1,2}

Because the + plus quantifier is greedy by default and would match:

9999323.32532553.893588972350897327

As well as:

1.1.1

Using curly braces gives allows you to be more specific and determine if the specified version isn't formatted properly.

If you wanted to, you could get rid of the second number and just use {1,} to match one or more numbers.

Upvotes: 1

MonkeyZeus
MonkeyZeus

Reputation: 20737

If you want to enforce the lower limit of 0.0.1 then this will work:

<?php
function is_valid_version( $version )
{
    // Assume the input is invalid
    $return = false;

    // Make sure we can explode() the input
    if( is_scalar( $version ) )
    {
        // Explode on periods
        $version = explode( '.', (string)$version );

        // We should have exactly items
        if( count( $version ) === 3 )
        {
            // All three items should be digits
            if(
                ctype_digit( $version[ 0 ] ) &&
                ctype_digit( $version[ 1 ] ) &&
                ctype_digit( $version[ 2 ] )
            )
            {
                // At least one digit needs to not be zero
                // 0.0.0 should be invalid
                // If you want the upper limit to be 99.99.99 then this is where you should do it
                if(
                    (int)$version[ 0 ] !== 0 ||
                    (int)$version[ 1 ] !== 0 ||
                    (int)$version[ 2 ] !== 0
                )
                {
                    $return = true;
                }
            }
        }
    }

    return $return;
}

var_dump( is_valid_version( '1.2.3' ) ); // true
var_dump( is_valid_version( '0.0.0' ) ); // false
var_dump( is_valid_version( '0.0.1' ) ); // true
var_dump( is_valid_version( '0.1.0' ) ); // true
var_dump( is_valid_version( '100.53456.fgdf' ) ); //false
var_dump( is_valid_version( array() ) ); //false

Upvotes: 2

shyammakwana.me
shyammakwana.me

Reputation: 5752

Simple regex would be \d+\.\d+\.\d+.

For php you can use preg_match. Check example on this link.

p.s. this is not strict check like version should be atleast 0.0.1 or max should be 99.99.99. It just checks for a pattern like below.

<number><dot><number><dot><number><dot>

Upvotes: 4

Zachary Craig
Zachary Craig

Reputation: 2220

Assuming, as your examples in your question, you just want to match exactly 3 sets of numbers (assuming they can only be 2 digits containing only 0-9) check my solution here

[0-9]{1,2}\.[0-9]{1,2}\.?[0-9]{1,2}

This however has a few issues, as it won't work on less than 3 sets of digits, now if the play store doesn't allow that, great, but be aware that with the above regex version numbers like 1.0 and 1.1 will not match

Upvotes: 3

Related Questions