you should also validate/check the value of $versionLength, because you subtract  (-1) in the next line. Can your regexp take a negative value?