PHP Get version from url

73 views Asked by At

How can I download the versions from the link? I tried to do it with regEx but it didn't work as I wanted.

I wish it would work like this (Where I gave NULL, there is nothing to choose from)

https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js > 1.11.2
https://ajax.googleapis.com/ajax/libs/jquery/v1.11.2/jquery.min.js > 1.11.2
https://ajax.googleapis.com/ajax/libs/jquery/version-1.11.2/jquery.min.js > 1.11.2
https://ajax.googleapis.com/ajax/libs/jquery/1-11-2/jquery.min.js > NULL
https://ajax.googleapis.com/ajax/libs/jquery/1112/jquery.min.js > NULL
https://www.google.com/recaptcha/api.js?render=6LdBsjgVaoTJ8rC-Npzz16bnAE > NULL

My regEx: /([0-9]+\.?)+/

Thank you in advance for your help :)

1

There are 1 answers

0
Wiktor Stribiżew On BEST ANSWER

You can use

\/(?:v(?:ersion)?-?)?\K\d+(?:\.\d+)+

See the regex demo. NOTE: if you want to make sure after the version number there is a / or end of string, add a (?![^\/]) lookahead at the end of the pattern, \/(?:v(?:ersion)?-?)?\K\d+(?:\.\d+)+(?![^\/]) (see this regex demo).

Details:

  • \/ - a / char
  • (?:v(?:ersion)?-?)? - an optional sequence of a v and then an optional ersion and then an optional - char - \K - omit the matched text
  • \d+(?:\.\d+)+ - match and consumer one or more digits and then one or more sequences of a dot and one or more digits.