Hi everyone,
I have a string for example like below:
Code:
id:3,name:example,ip:89.12.4.54,sa:false,a:false;id:5,name:exampletwo,ip:78.23.45.61,sa:false,a:true;
I am using a preg_match_all function to retrieve the string pattern into variables.
However, the following doesnt seem to work properly. It works when there is one of the pattern but if more than one the array is created incorrectly.
I am inexperienced with preg_match_all, anyone spot how to just isolate the pattern correctly?
My code:
Code:
// Get array
function get_vars($var)
{
$vars = array();
$pattern = "/id:(.*),name:(.*),ip:(.*),sa:(.*),a:(.*);/si"; // This is the pattern to retrieve in the string
if (preg_match_all ($pattern , $var, $matches, PREG_SET_ORDER))
{
foreach ($matches as $var)
{
$vars[] = array
(
'id' => $var[1],
'name' => htmlspecialchars_decode(trim($var[2])),
'ip' => $var[3],
'sa' => $var[4],
'a' => $var[5]
);
}
}
return $vars;
}
Any help most appreciated.
Matt