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. Some开发者_JAVA百科thing 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 (").
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);
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
}
This should be done with a regular expression. See the preg_match function for this.
explode('-', str_replace('"', '', str_replace('" "', '"-"', $data)));
精彩评论