我有一个包含一些html实体的字符串
<listing name="name goes there" phone="321321" >Text description</listing>
<anytag name="another name" phone="any phone" attr1="value 1" attr2="value 2">any can be written where ©MyRight</anytag>
<anytag name="another name line 2" phone="65851566" attr1="value ë" attr2="value 2">any can be written where ®MyRightëous</anytag>
我希望在PHP的单个正则表达式中获取这些元素的所有属性,
如果我尝试HtmlDom
,则会向我显示undefined tags
的错误,如果我使用SimpleXml
,则会拒绝解析html实体。
所以我虽然尝试使用RegExp但却无法找到解决方案。
RegExp以外的解决方案也欢迎。
答案 0 :(得分:4)
您可以使用以下基于DOM解析器的代码列出给定标记名称的所有属性:
$str = <<<EOF
<listing name="name goes there" phone="321321" phone="any phone" attr1="value 1" attr2="value 2">Text description</listing>
<anytag name="another name" phone="any phone" attr1="value 1" attr2="value 2">any can be written where ©MyRight</anytag>
<anytag name="another name line 2" phone="65851566" attr1="value ë" attr2="value 2">any can be written where ®MyRightëous</anytag>
EOF;
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($str);
$nodeList = $dom->getElementsByTagName('anytag');
for($i=0; $i < $nodeList->length; $i++) {
$node = $nodeList->item($i);
if ($node->hasAttributes())
echo $node->nodeName . " =>\n";
foreach ($node->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
echo "Attribute '$name'='$value'\n";
}
}
答案 1 :(得分:-1)
怎么样:
<?php
$str = 'your string here';
$lines = explode("\n", $str);
foreach ($lines as $line){
preg_match_all("@\s+(?<attr_name>)\w+\=\"(?<attr_value>[^\"]+)\"@msi", $line, $results);
echo "<pre>";
print_r($results);
echo "</pre>";
}
?>