phpwordpressversionhtml-parsingmeta-tags

Get WordPress version from meta tag with name="generator"


I have never understood the pattern of regular expression and after googling I haven't been any wiser.

I want to grab the WordPress version number (3.2) from this string:

<meta name="generator" content="WordPress 3.2" />

In the future when upgrading to 3.3 I wan't the split code to be able to get that to. So no static expression.

How do I solve this?


Solution

  • Here is a regular expression that works for this...

    $str = '<meta name="generator" content="WordPress 3.2" />';
    preg_match('/meta name="generator" content="WordPress [0-9]+\.[0-9]" /', $str, $matches);
    preg_match('/[0-9]+\.[0-9]/', $matches[0], $matches1);
    $version = $matches1[0];
    echo "Wordpress version is = $version";
    

    It should output this:

    Wordpress version is = 3.2