如果我没记错的话,这应该可以解决问题:
preg_match_all('/height(\:|\=)"*\s*([0-9]+[^;"]+);*/i','<tr style="height: 123px; border: none;><tr height="125px"',$matches);
var_dump($matches[2]);//array('123px','125px');
但是由于您要让这个正则表达式在 HTML 上松散(如果我没记错的话),我会研究解析DOM的方法并使用DOMElement 的方法来获得我想要的东西。这是对这个问题的更强有力的看法。
根据 OP 的要求:
function getDeepChildren($node,&$nodeArray)
{//recursive function to flatten dom
$current = $node->getElementsByTagName('*');//get all children
foreach($current as $node)
{//loop through children
$nodeArray[] = $node;//add child
if ($node->hasChildNodes())
{//if child node has children of its own
getDeepChildren($node,$nodeArray);//get the children and append to nodeArray
}
}
}//no return value, $nodeArray is passed by reference
$dom = new DOMDocument();
$dom->loadHTML($body);
$nodes = array();
getDeepChildren($dom,$nodes);//$nodes is passed by reference
$height = array();
while($node = array_shift($nodes))
{//$height[i][0] === height value, $height[i][1] is reference to node
if ($node->hasAttribute('height'))
{
$height[] = array($node->getAttribute('height'),$node);
continue;//already got what we need, no need for slow preg_match
//in case of <div height="123px" style="border:1px solid #F00;"> for example...
}
if ($node->hasAttribute('style') && preg_match('/height\s*\:\s*([0-9]+\s*[a-z]+)\s*;/i',$node->getAttribute('style'),$match))
{
$height[] = array($match[1],$node);
}
}
var_dump($height);//should contain everything you're looking for
对于更面向对象的方法,我建议查看几个递归 domnode 迭代器类。
不鼓励通过引用传递数组,但这是获得所需内容的最简单方法。一个替代版本是:
function getDeepChildren($node)
{
$nodes = array();
$current = $node->getElementsByTagName('*');
foreach($current as $node)
{
$nodes[] = $node;
if ($node->hasChildNodes())
{
$nodes = array_merge($nodes,getDeepChildren($node));
}
}
return $nodes;
}
//instead of getDeepChildren($dom,$nodes), usage is:
$nodes = getDeepChildren($dom);