Reputation: 3620
I have retrieved the following output from a service.
Output:
OK: set username OK: set password OK: set server state acquiring-network OK: sign-in OK: get-group-members group contact-list contact 5551000008539 name "Driver 2" state offline group contact-list contact 5551000008540 name "Driver 3" state offline group contact-list contact 5551000008541 name "Driver 4" state offline state connecting client-own-id 5551000008535 client-own-id 0 client-own-id 5551000008535 state disconnecting sign-in denied auth-error client-own-id 0 state offline
How can i extract the data into the user array as before?
how can i explode/implode/preg match or whatever into an array of users. like:
$users - Driver 2 => 5551000008539
Driver 3 => 5551000008540
Driver 4 => 5551000008541
Upvotes: 0
Views: 113
Reputation: 272086
<?php
$str = 'OK: set username OK: set password OK: set server state acquiring-network OK: sign-in OK: get-group-members group contact-list contact 5551000008539 name "Driver 2" state offline group contact-list contact 5551000008540 name "Driver 3" state offline group contact-list contact 5551000008541 name "Driver 4" state offline state connecting client-own-id 5551000008535 client-own-id 0 client-own-id 5551000008535 state disconnecting sign-in denied auth-error client-own-id 0 state offline';
preg_match_all('@contact (\d+) name "(.+?)"@', $str, $matches, PREG_SET_ORDER);
print_r($matches);
$array = array();
foreach($matches as $match) {
$array[$match[2]] = $match[1];
}
print_r($array);
Salvaging the data from $matches is left as an exercise.
Upvotes: 1
Reputation: 8490
Try this (assuming $s
contains the output from our service):
preg_match_all("/contact-list contact (\\d+) name \"(.*?)\"/", $s, $out, PREG_PATTERN_ORDER);
And to create an array with names as keys and the numbers as values:
$result = array_combine($out[2], $out[1]);
Upvotes: 0