Created
November 27, 2013 16:03
-
-
Save rsanchez/7678167 to your computer and use it in GitHub Desktop.
attributes_to_array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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