1

我有一个包含一些 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 以外的解决方案也受到欢迎。

4

2 回答 2

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

于 2013-03-14T08:37:32.897 回答
-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>";
  }

?>
于 2013-03-14T08:42:45.533 回答