Howdy_McGee Howdy_McGee - 1 year ago 77
PHP Question

Test if String Given is A Version Number

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

function but that looks like compareing two versions to one another.

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

Answer Source

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:

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

Which filters out 'non-version' strings.

For example:

if( version_compare( $_POST['_iq_plugin_vers'], '0.0.1', '>=' ) >= 0 ) {
    echo 'Valid Version';
} else {
    echo 'Invalid Version';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download