从标记中筛选属性的PHP正则表达式


PHP regular expression to filter attribute from tag

我使用PHP脚本将一些自定义标记转换为有效的Bootstrap标记,因为我需要将它们放在多个脚本中,并且只需要一个地方来管理设置。

例如,将<well>转换为<div class="well">很简单,但如果我想将<well title="hello">转换为<div class="well"><h4>hello</h4>,我应该有什么模式?

因此,我需要能够识别具有可以具有任何值的属性的标签,但我也需要具有该值。一旦事情变得更加复杂,Regex对我来说仍然是个谜。。。

谢谢!

它不是很动态,但适用于这个特定的例子。。。这是你需要的吗?

$re = "/''<([''w]+) ?(([''w]+)''='''"([''w]+)'''")''>/"; 
$str = "<well title='"hello'">";
preg_match($re, $str, $matches);
print_r($matches);
$new_tag = '<div class="'.$matches['1'].'"><h4>'.$matches[4].'</h4></div>';
echo "'n".$new_tag;