views:

103

answers:

4

Using PHP, I'm trying to give each specific text its own variable. I believe this can be achieved by using the explode list function in php. Something similar to the code below:

list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);

However, the above code separates the text by using a colon (:). The text I'd like to separate are within quotes, for example "WORD". The example text I'd like to separate is as below:

"AULLAH1" "01/07/2010 15:28 " "55621454" "123456" "123456.00"

I'd like the text/numbers AULLAH1, 01/07/2010 15:28, 55621454, 123456, 123456.00 to all have a specific PHP variable. If possible, I'd like the PHP explode feature to separate the content by a beginning quote (") and an ending quote (").

+2  A: 

A better way is to use preg_match_all:

$s = '"AULLAH1" "01/07/2010 15:28 " "55621454" "123456" "123456.00"';
preg_match_all('/"([^"]*)"/', $s, $matches);
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = $matches[1];

The most simliar way would be to use preg_split:

list($user, $pass, $uid, $gid, $gecos, $home, $shell) =
    preg_split('/"(?: ")?/', $s, -1, PREG_SPLIT_NO_EMPTY);
Artefacto
@Timwi, It doesn't fail; assigning to `list` gives `NULL` to `$home` and `$shell` in this case.
strager
@strager Was it supposed to fail? The OP doesn't mention that and it's consistent with his current solution.
Artefacto
Ah OK, you were responding to a now deleted comment.
Artefacto
Hi Artefacto, I extremely appreciate your response and I'm grateful for all the effort you've put into your reply. I've used the preg_match_all method and it seems to work more than perfectly. Also, I liked the idea of using preg_split too, as for now I'll stick to the first method. Again, I appreciate your response. Thank you for your help. ;)
AUllah1
+1  A: 

This is the simplest solution, but certainly not the most robust:

$data = '"AULLAH1" "01/07/2010 15:28 " "55621454" "123456" "123456.00"';

list($user, $pass, $uid, $gid, $gecos, $home, $shell)
    = explode('" "', trim($data, '"'));

var_dump(array($user, $pass, $uid, $gid, $gecos, $home, $shell));

// gives:
array(7) {
  [0]=>
  string(7) "AULLAH1"
  [1]=>
  string(17) "01/07/2010 15:28 "
  [2]=>
  string(8) "55621454"
  [3]=>
  string(6) "123456"
  [4]=>
  string(9) "123456.00"
  [5]=>
  NULL
  [6]=>
  NULL
}
strager
Hi Strager, I extremely appreciate your response as well as your efforts. In all honesty, I don't like the idea of going into arraying the data, although I do understand that it can be extremely useful at times. Sorry if I seem to be giving you a negative vibe, as stated before, I am grateful that you responded. ;) Thank you.
AUllah1
@AUl The arraying was just for displaying purposes.
Artefacto
@AUllah1, @Artefacto is correct; I just put the elements into an array to easily view the `var_dump` to show that it works as expected.
strager
Ahh, that's great. Apologies for the assumption strager. That seems like some amazing coding. I appreciate your response. ;) Thank you.
AUllah1
+1  A: 

This should be done with a regular expression. See the preg_match function for this.

eKek0
A: 
explode('-', str_replace('"', '', str_replace('" "', '"-"', $data)));
Omar
This will fail on `"01/07/2010 15:28 "`.
strager
Hi Omar, thank you for your response. I extremely appreciate it. As mentioned by strager, it did fail at that specific location. :( Again, I really do appreciate your response as well as your effort. ;)
AUllah1
Alright. Modified it.
Omar