1

使用 PHP,我试图为每个特定文本赋予其自己的变量。我相信这可以通过使用 php.ini 中的爆炸列表功能来实现。类似于下面的代码:

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

但是,上面的代码使用冒号 ( :) 分隔文本。我想分隔的文本在引号内,例如"WORD". 我想分开的示例文本如下:

“AULLAH1”“01/07/2010 15:28”“55621454”“123456”“123456.00”

我希望 text/numbers AULLAH1, 01/07/2010 15:28, 55621454, 123456,123456.00都有一个特定的 PHP 变量。如果可能的话,我希望 PHP 分解功能通过开头引号 (") 和结尾引号 (") 来分隔内容。

4

4 回答 4

3

更好的方法是使用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];

最类似的方法是使用preg_split

list($user, $pass, $uid, $gid, $gecos, $home, $shell) =
    preg_split('/"(?: ")?/', $s, -1, PREG_SPLIT_NO_EMPTY);
于 2010-08-21T23:32:55.557 回答
1

这是最简单的解决方案,但肯定不是最强大的:

$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
}
于 2010-08-21T23:33:25.380 回答
1

这应该使用正则表达式来完成。请参阅preg_match函数。

于 2010-08-21T23:34:58.107 回答
0
explode('-', str_replace('"', '', str_replace('" "', '"-"', $data)));
于 2010-08-21T23:35:17.483 回答