12530 fjikd ftp /etc/ftponly conakry.dreamhost.com uij545 0 0 rtgfg Tian就这样一行字符串,明明使用tab分隔的啊,我也用editplus查找了,确实是tab啊,但是我用explode('\t', $user_array)出来却不行啊。分割之后只有一个元素,说明分割符没找到啊,真是奇怪了呢。
<?php
$str = '12530 fjikd ftp /etc/ftponly conakry.dreamhost.com uij545 0 0 rtgfg Tian';
print_r(explode('\t', $str));
echo '<br />';
print_r(preg_split('/\t/', $str));
echo '<br />';
print_r(split('\t', $str));
?>
运行得到的结果:
Array ( [0] => 12530 fjikd ftp /etc/ftponly conakry.dreamhost.com uij545 0 0 rtgfg Tian )
Array ( [0] => 12530 [1] => fjikd [2] => ftp [3] => /etc/ftponly [4] => conakry.dreamhost.com [5] => uij545 [6] => 0 [7] => 0 [8] => rtgfg Tian )
Array ( [0] => 12530 fjikd f [1] => p /e [2] => c/f [3] => ponly conakry.dreamhos [4] => .com uij545 0 0 r [5] => gfg Tian )
难道explode完全只能分割字面的分割符吗?这就是说explode和split都不能用来分割tab了?
<?php
$str = '12530 fjikd ftp /etc/ftponly conakry.dreamhost.com uij545 0 0 rtgfg Tian';
print_r(explode('\t', $str));
echo '<br />';
print_r(preg_split('/\t/', $str));
echo '<br />';
print_r(split('\t', $str));
?>
运行得到的结果:
Array ( [0] => 12530 fjikd ftp /etc/ftponly conakry.dreamhost.com uij545 0 0 rtgfg Tian )
Array ( [0] => 12530 [1] => fjikd [2] => ftp [3] => /etc/ftponly [4] => conakry.dreamhost.com [5] => uij545 [6] => 0 [7] => 0 [8] => rtgfg Tian )
Array ( [0] => 12530 fjikd f [1] => p /e [2] => c/f [3] => ponly conakry.dreamhos [4] => .com uij545 0 0 r [5] => gfg Tian )
难道explode完全只能分割字面的分割符吗?这就是说explode和split都不能用来分割tab了?
print_r(preg_split('/\s+/', $str));
preg_split已经得到正确结果了哦,我只是说explode和split不能得到正确结果。
$str = '12530 fjikd ftp /etc/ftponly conakry.dreamhost.com uij545 0 0 rtgfg Tian';
print_r(explode("\t", $str));
explode不能得到正确结果是因为你用单引号'\t',一个转义字符+一个t, 改用双引号"\t"才是tab字符。
另外发帖子的时候tab论坛把tab全部换成空格了。