1

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

3
  • In the end result or in the incoming data? Commented Aug 21, 2010 at 23:33
  • (1) Do you have any control over how these files are created? (2) Have you considered using an actual database? Commented Aug 21, 2010 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. ;) Commented Aug 21, 2010 at 23:40

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

@Timwi, It doesn't fail; assigning to list gives NULL to $home and $shell in this case.
@strager Was it supposed to fail? The OP doesn't mention that and it's consistent with his current solution.
Ah OK, you were responding to a now deleted comment.
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. ;)
1

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
}

4 Comments

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.
@AUl The arraying was just for displaying purposes.
@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.
Ahh, that's great. Apologies for the assumption strager. That seems like some amazing coding. I appreciate your response. ;) Thank you.
1

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

1 Comment

This is low-quality and is better positioned as a comment.
0
explode('-', str_replace('"', '', str_replace('" "', '"-"', $data)));

2 Comments

This will fail on "01/07/2010 15:28 ".
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. ;)

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.