1

I have a string that contains some html entities

<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>

I want to get all the attributes of these elements in a single regex for PHP,

If I try HtmlDom it gives me error of undefined tags and if I use SimpleXml it denies to parse html entities.

So I though to try RegExp but could not find solution to that.

Solutions other than RegExp also welcome.

2 Answers2

4

You can use following DOM parser based code to list all attributes of a given tag name:

$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";
       }
}

Live Demo: http://ideone.com/k8SLhr

anubhava
  • 761,203
  • 64
  • 569
  • 643
-1

how about this:

<?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>";
  }

?>
Anton
  • 1,029
  • 7
  • 19