John Davidson

regex - PHP Get version from url

0 comments
Message:


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 :)



My Answer:
You can use regular expressions in PHP to extract the version number from the URL. Here's an example code snippet that demonstrates how you can achieve this:

php
$url = 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js';

// Define the regex pattern to match the version number
$pattern = '/\/(\d+\.\d+\.\d+)\//';

// Perform the regex match
preg_match($pattern, $url, $matches);

// Extract the version number from the matched result
$version = isset($matches[1]) ? $matches[1] : NULL;

// Output the version number
echo $version;


In this code snippet, we define a regex pattern that matches a version number in the format of "x.x.x" within the URL. We then use `preg_match()` function to perform the regex match on the URL and extract the version number from the matched result. Finally, we output the extracted version number.

You can adjust the regex pattern as needed to match different version number formats in the URL.

Rate this post

4 of 5 based on 9606 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us