pls. I have problem using regexp expresion in the following php function:
$xml1 = "<arg1>
<S113-03>1</S113-03>
<S184-06>1</S184-06>
</arg1>";
$xml2 = "<arg1>
<P055>1</P055>
<P096>1</P096>
</arg1>";
function xml2array($xml) {
$xmlArray = array();
$regexp = "/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s";
preg_match_all($regexp, $xml, $elements);
foreach ($elements[1] as $ie => $element) {
if (preg_match($regexp, $elements[3][$ie]))
$xmlArray[$element] = xml2array($elements[3][$ie]);
else {
$xmlArray[$element] = trim($elements[3][$ie]);
}
}
return $xmlArray;
}
$array = xml2array($xml1);
echo print_r($array, true);
while $xml2 gives me result (it is OK):
Array
(
[arg1] => Array
(
[P055] => 1
[P096] => 1
)
)
while $xml1 gives me result (wrong):
Array
(
[arg1] => <S113-03>1</S113-03>
<S184-06>1</S184-06>
)
I believe the problem is in regexp, but its content seems to be chinesse tea for me