In PHP what is the most efficient way to parse this string into an associative array?
%STCITY^LASTNAME$FIRSTNAME$MIDDLENAME^ADDRESS1$ADDRESS2^?;UNIQUEID=YYMMDDYYMMDD=?
In PHP what is the most efficient way to parse this string into an associative array?
%STCITY^LASTNAME$FIRSTNAME$MIDDLENAME^ADDRESS1$ADDRESS2^?;UNIQUEID=YYMMDDYYMMDD=?
Similar to Igor:
$regex = '/%(?<State>..)(?<City>[^^]*)\^(?<LastName>[^$]*)\$(?<FirstName>[^$]*)\$(?<MiddleName>[^^]*)\^(?<Address1>[^$]*)\$(?<Address2>[^^]*)\^\?;(?<UniqueId>[^=]*)=(?<Expiration>.{6})(?<Birthday>.{6})=/';
preg_match($regex, $str, $matches);
If you run the example:
array (
0 => '%STCITY^LASTNAME$FIRSTNAME$MIDDLENAME^ADDRESS1$ADDRESS2^?;UNIQUEID=YYMMDDYYMMDD=',
'State' => 'ST',
1 => 'ST',
'City' => 'CITY',
2 => 'CITY',
'LastName' => 'LASTNAME',
3 => 'LASTNAME',
'FirstName' => 'FIRSTNAME',
4 => 'FIRSTNAME',
'MiddleName' => 'MIDDLENAME',
5 => 'MIDDLENAME',
'Address1' => 'ADDRESS1',
6 => 'ADDRESS1',
'Address2' => 'ADDRESS2',
7 => 'ADDRESS2',
'UniqueId' => 'UNIQUEID',
8 => 'UNIQUEID',
'Expiration' => 'YYMMDD',
9 => 'YYMMDD',
'Birthday' => 'YYMMDD',
10 => 'YYMMDD',
)
As you can see, it has string keys based on the named groups (as well as numeric).