开发者

Split string PHP

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

I want to grab the WordPress versi开发者_JS百科on 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?


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


preg_match('|<meta name="generator" content="WordPress (.*?)" />|', $where_to_search_for, $match);
print_r($match);


$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";
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜