0

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
2
  • 1
    You probably need to post the exact format of the input string (including newlines) to get a valid answer. Commented Mar 6, 2012 at 20:40
  • Perhaps ":" is a separator. But clarification is needed before I'll try to answer this. Commented Mar 6, 2012 at 20:40

2 Answers 2

1
<?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.

Sign up to request clarification or add additional context in comments.

1 Comment

just got 2 empty arrays.. Array ( ) Array ( )
0

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]);

2 Comments

Warning: array_combine() [function.array-combine]: Both parameters should have at least 1 element
assign $s = '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';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.