3

I would like to extract the version number from the url content. i tried to extract info using curl_exec. but unable to get the preg_match to get the exact info.

Code i tried is

function getVersionFromurl(string $url)
{
    $curl = curl_init($url);
    $content = curl_exec($curl);
    curl_close($curl);
    $rx = preg_match("(\"version\" (\d+\.\d+\.\d+\.\d+))", $content, $matches);
    echo $matches[1];
}

$url = 'https://www.foxitsoftware.com/downloads/downloadForm.php?retJson=1&product=Foxit-Reader&platform=Mac-OS-X';
$val = getVersionFromurl($url);

here the content came as

"{"package_info":{"language":["English","French","German","Italian","Spanish"],"type":["pkg"],"version":["3.4.0.1012","2.1.0804","2.0.0625","1.1.1.0301","1.1.0.0128"],"size":"139.13MB","release":"10/15/19","os":"","down":"/pub/foxit/reader/desktop/mac/3.x/3.4/ML/FoxitReader340.setup.pkg","mirror":"","manual":"","big_version":"3.x"}}1"

How to extract 3.4.0.1012 from the content. the preg_matcxh i tried gives me error. how to write the preg_match regular expression.

Please any help.

1

2 Answers 2

3

better to convert string to JSON object, and get version value from there

function getVersionFromurl(string $url)
{
    $curl = curl_init($url);
    $content = curl_exec($curl);
    curl_close($curl);

    $contentObj = json_decode($content);
    echo $contentObj.package_info.version[0];
}

$url = 'https://www.foxitsoftware.com/downloads/downloadForm.php?retJson=1&product=Foxit-Reader&platform=Mac-OS-X';
$val = getVersionFromurl($url);

Notice this conversion from string into object, and get first element of version array

$contentObj = json_decode($content);
echo $contentObj.package_info.version[0];
Sign up to request clarification or add additional context in comments.

Comments

1

In your pattern you are not accounting for this part :\[" after "version" You are also using 2 capturing groups, so $matches[1] would then contain the whole match and $matches[2] would contain your value.

But instead, you can use 1 capturing group.

"version":\["(\d+(?:\.\d+){3})"

Regex demo | Php demo

preg_match('~"version":\["(\d+(?:\.\d+){3})"~', $content, $matches);
echo $matches[1];

Output

3.4.0.1012

Note that you don't have to escape the double quotes and that you have to use delimiters for the pattern.

2 Comments

i get the error 'PHP Notice: Undefined offset: 1 in /workspace/Main.php on line 8" . for "echo $matches[1];" it looks like preg_match returned nothing.
Are you sure you have the same payload? What does the string look like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.