Skip to content

Instantly share code, notes, and snippets.

@rsanchez
Created November 27, 2013 16:03
Show Gist options
  • Save rsanchez/7678167 to your computer and use it in GitHub Desktop.
Save rsanchez/7678167 to your computer and use it in GitHub Desktop.
attributes_to_array
/**
* Convert a string of attributes to an associative array
*
* @param string $attributes_string a string of XML/HTML attributes, ex. width="100" height="200"
* @return array ex. array('width' => '100', 'height' => '200')
*/
function attributes_to_array($attributes_string) {
$attributes = array();
// use simplexml to parse the attributes string
$xml = new SimpleXMLElement('<element'.$attributes_string.'></element>');
//loop through all attributes and collect them
foreach ($xml->attributes() as $key => $value) {
$attributes[$key] = (string) $value;
}
return $attributes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment