正则表达式 - 获取元素的所有属性

时间:2013-03-14 08:25:50

标签: php regex pcre

我有一个包含一些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&nbsp; where &copy;MyRight</anytag>
<anytag name="another name line 2" phone="65851566" attr1="value &euml;" attr2="value 2">any can be written&nbsp; where &reg;MyRight&euml;ous</anytag>

我希望在PHP的单个正则表达式中获取这些元素的所有属性,

如果我尝试HtmlDom,则会向我显示undefined tags的错误,如果我使用SimpleXml,则会拒绝解析html实体。

所以我虽然尝试使用RegExp但却无法找到解决方案。

RegExp以外的解决方案也欢迎。

2 个答案:

答案 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&nbsp; where &copy;MyRight</anytag>
<anytag name="another name line 2" phone="65851566" attr1="value &euml;" attr2="value 2">any can be written&nbsp; where &reg;MyRight&euml;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";
       }
}

现场演示:http://ideone.com/k8SLhr

答案 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>";
  }

?>