Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 (").

share|improve this question
    
In the end result or in the incoming data? –  Pekka 웃 Aug 21 '10 at 23:33
    
(1) Do you have any control over how these files are created? (2) Have you considered using an actual database? –  quantumSoup Aug 21 '10 at 23:34
    
Pekka: Thank you for your response, I extremely appreciate it. I'd like it as an incoming data. Again, thank you for your response. :) quantumSoup: Firstly, thank you for your response, I extremely appreciate them. ;) (1) It's only the one file, there are a lot of steps besides this. Finding away to give the following a variable is all I need at the moment. (2) I've been told that before, but unfortunately, I don't have the ability to do so. Thank you for your response. ;) –  AUllah1 Aug 21 '10 at 23:40

4 Answers 4

up vote 2 down vote accepted

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);
share|improve this answer
    
@Timwi, It doesn't fail; assigning to list gives NULL to $home and $shell in this case. –  strager Aug 21 '10 at 23:35
    
@strager Was it supposed to fail? The OP doesn't mention that and it's consistent with his current solution. –  Artefacto Aug 21 '10 at 23:39
    
Ah OK, you were responding to a now deleted comment. –  Artefacto Aug 21 '10 at 23:41
    
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 Aug 21 '10 at 23:48

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
}
share|improve this answer
    
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 Aug 21 '10 at 23:44
    
@AUl The arraying was just for displaying purposes. –  Artefacto Aug 22 '10 at 0:09
    
@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 Aug 22 '10 at 0:35
    
Ahh, that's great. Apologies for the assumption strager. That seems like some amazing coding. I appreciate your response. ;) Thank you. –  AUllah1 Aug 22 '10 at 13:46

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

share|improve this answer
explode('-', str_replace('"', '', str_replace('" "', '"-"', $data)));
share|improve this answer
    
This will fail on "01/07/2010 15:28 ". –  strager Aug 21 '10 at 23:37
    
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 Aug 21 '10 at 23:46
    
Alright. Modified it. –  owahab Aug 21 '10 at 23:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.