views:

2303

answers:

4

Hi,

I've got a string with HTML attributes:

$attribs = ' id= "header " class = "foo   bar" style ="background-color:#fff; color: red; "';

How to transform that string into an indexed array, like:

array(
  'id' => 'header',
  'class' => array('foo', 'bar'),
  'style' => array(
    'background-color' => '#fff',
    'color' => 'red'
  )
)

so I can use the PHP array_merge_recursive function to merge 2 sets of HTML attributes.

Thank you

+2  A: 

You could use a regular expression to extract that information:

$attribs = ' id= "header " class = "foo   bar" style ="background-color:#fff; color: red; "';
$pattern = '/(\\w+)\s*=\\s*("[^"]*"|\'[^\']*\'|[^"\'\\s>]*)/';
preg_match_all($pattern, $attribs, $matches, PREG_SET_ORDER);
$attrs = array();
foreach ($matches as $match) {
    if (($match[2][0] == '"' || $match[2][0] == "'") && $match[2][0] == $match[2][strlen($match[2])-1]) {
        $match[2] = substr($match[2], 1, -1);
    }
    $name = strtolower($match[1]);
    $value = html_entity_decode($match[2]);
    switch ($name) {
    case 'class':
        $attrs[$name] = preg_split('/\s+/', trim($value));
        break;
    case 'style':
        // parse CSS property declarations
        break;
    default:
        $attrs[$name] = $value;
    }
}
var_dump($attrs);

Now you just need to parse the classes of class (split at whitespaces) and property declarations of style (a little bit harder as it can contain comments and URLs with ; in it).

Gumbo
Thank you Gumbo, your regex is cool.The only problem is $attrs['class'] or $attrs['style'] are returning strings: so it will be difficult to merge them with another $attribs string, for example merging that 2 sets of attribs:$attribs1 = 'class="foo bar"';$attribs2 = 'class="lorem"';into a 'class="foo bar lorem"'That's why I would like $attrs['class'] returns an array: array('foo', 'bar')Do you have an idea to enhance this?
abernier
+3  A: 

May be this helps you .. What it does ..

  • A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way!
  • Require PHP 5+.
  • Supports invalid HTML.
  • Find tags on an HTML page with selectors just like jQuery.
  • Extract contents from HTML in a single line.

http://simplehtmldom.sourceforge.net/

Wbdvlpr
A: 

You can't use a regular expression to parse html-attributes. This is because the syntax is contextual. You can use regular expressions to tokenize the input, but you need a state machine to parse it.

If the performance isn't a big deal, the safest way to do it, is probably to wrap the attributes in a tag and then send it through an html parser. Eg.:

function parse_attributes($input) {
  $dom = new DomDocument();
  $dom->loadHtml("<foo " . $input. "/>");
  $attributes = array();
  foreach ($dom->documentElement->attributes as $name => $attr) {
    $attributes[$name] = $node->value;
  }
  return $attributes;
}

You could probably optimize the above, by reusing the parser, or by using XmlReader or the sax parser.

troelskn
Parse this: foo='bar' cuux="O'Reiley" zip="\"zap\""
troelskn
@troelskn: The third attribute value declaration is invalid. The `"` need to be represented by character references.
Gumbo
You're right - I wasn't aware of that. I would still suggest using an xml/html parser, to account for all sorts of odd edge cases.
troelskn
+1  A: 

Use SimpleXML:

<?php
$attribs = ' id= "header " class = "foo   bar" style ="background-color:#fff; color: red; "';

$x = new SimpleXMLElement("<element $attribs />");

print_r($x);

?>

This assumes that the attributes are always name/value pairs...

Ken Keenan