获取WordPress版本的正则表达式

时间:2013-01-18 18:57:19

标签: php regex wordpress parsing

我想如果有人可以帮助我使用正则表达式或PHP中的任何方法来获取readme.html文件中的WordPress版本,就像这样:

<h1 id="logo" style="text-align: center">
<img alt="WordPress" src="wp-admin/images/wordpress-logo.png" />
<br /> Version 2.8.1
 </h1>

谢谢!

2 个答案:

答案 0 :(得分:4)

Pure Regex

preg_match("/Version\s+([\d.]+)/", $html, $match);
$version = $match[1];

使用DOMDocument

$doc = new DOMDocument;
$doc->loadHTML($html);
$text = trim($doc->getElementsByTagName("h1")->item(0)->textContent);
$index = strrpos($text, " ");
$version = substr($text, $index+1);

答案 1 :(得分:0)

你不需要那个。而是使用get_bloginfo('version');

get_bloginfo('version') - 返回您使用的WordPress版本。此数据是从wp-includes / version.php中设置的'$ wp_version'变量中检索的。 http://codex.wordpress.org/Function_Reference/get_bloginfo