1

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?

Richie Cotton
  • 113,548
  • 43
  • 231
  • 352
Fredrik
  • 597
  • 5
  • 13
  • 28
  • 2
    The proper way would be to use a [HTML parser](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php/3577662#3577662), even though it might seem like overkill for the task – Pekka Jul 12 '11 at 17:26
  • It might be overkill, but it would be a lot more reliable in the long run. If WP changes whitespace or what have you, the regex could break. – Anthony Jul 12 '11 at 17:29

4 Answers4

2

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

Patrick
  • 3,187
  • 4
  • 26
  • 45
0

Though it's pretty old question, and out of curiosity, why don't you just use builtin function to retreve the WordPress version being used?

<?php echo get_bloginfo( 'version' );?>

This way, even the Generator meta is removed, you will get the exact version from $wp_version var.

Abhik
  • 622
  • 1
  • 21
  • 39
0
$data = '<meta name="generator" content="WordPress 3.2" />';
$pat = '<meta name="generator" content="WordPress (\d*\.?\d*)" />';
if(($match = preg_match($pat, $data)) !== false){
    echo $match[1];
}else{
    echo "not found";
}
Ilia Choly
  • 17,390
  • 14
  • 87
  • 153
0
preg_match('|<meta name="generator" content="WordPress (.*?)" />|', $where_to_search_for, $match);
print_r($match);
genesis
  • 49,367
  • 20
  • 94
  • 122