skip to Main Content

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.

2

Answers


  1. 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];
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search