JUtility/parseAttributes: Difference between revisions
From Joomla! Documentation
m clean up |
m preparing for archive only |
||
| Line 4: | Line 4: | ||
===Syntax=== | ===Syntax=== | ||
| Line 52: | Line 52: | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=parseAttributes | category=parseAttributes | ||
category=JUtility | category=JUtility | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
Latest revision as of 02:16, 25 March 2017
Description
Method to extract key/value pairs out of a string with xml style attributes
Syntax
static parseAttributes($string)
| Parameter Name | Default Value | Description |
|---|---|---|
| $string | $string String containing xml style attributes |
Returns
array Key/Value pairs for the attributes
Defined in
libraries/joomla/utilities/utility.php
Importing
jimport( 'joomla.utilities.utility' );
Source Body
public static function parseAttributes($string)
{
// Initialise variables.
$attr = array();
$retarray = array();
// Lets grab all the key/value pairs using a regular expression
preg_match_all('/([\w:-]+)[\s]?=[\s]?"([^"]*)"/i', $string, $attr);
if (is_array($attr))
{
$numPairs = count($attr[1]);
for ($i = 0; $i < $numPairs; $i++)
{
$retarray[$attr[1][$i]] = $attr[2][$i];
}
}
return $retarray;
}
Examples
Code Examples