Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if String Given is A Version Number

Tags:

php

version

Is there a way to test if a given string is a versioning number or not? I have some user input and I need to verify that the string being given to me can be used as a versioning number. I've seen that PHP has a version_compare() function but that looks like compareing two versions to one another.

I am assuming the given string should be a "PHP-Standardized" version.

like image 512
Howdy_McGee Avatar asked Mar 06 '15 16:03

Howdy_McGee


1 Answers

I'm not sure if this is the best way but using the version_compare() I just ensured that it was at least 0.0.1 by filtering out 'non-version' strings:

version_compare( $given_version, '0.0.1', '>=' )

For example:

if( version_compare( $_POST['_plugin_vers'], '0.0.1', '>=' ) >= 0 ) {
    echo 'Valid Version';
} else {
    echo 'Invalid Version';
}
like image 198
Howdy_McGee Avatar answered Oct 23 '22 02:10

Howdy_McGee