-1

I have an array which the format is as followed:

Array
(
    [1] => 
Status   Name               DisplayName                           
    [2] => 
------   ----               -----------                           
    [3] => 
Running  ADWS               Active Directory Web Services         
)

There is no key of the value of 0 as this is unset prior to displaying the Array, this array is generated from a text file:

$File = utf8_encode(file_get_contents("Services.txt"));

Now, Lets take the third key within this array:

    [3] => 
Running  ADWS               Active Directory Web Services   

How would I explode at tab space so I get:

array
(
   [1] => Running
   [2] => ADWS
   [3] => Active Directory Web Services
)

I am currently exploding at a white space, which is generating the wrong output... How would I go about this?


Using a regex I get the following:

 preg_split('/\s+/', $String);




Array
(
[0] => Array
    (
        [0] => 
        [1] => Running
        [2] => 
        [3] => ADWS
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
        [15] => 
        [16] => 
        [17] => 
        [18] => Active
        [19] => Directory
        [20] => Web
        [21] => Services
        [22] => 
        [23] => 
        [24] => 
        [25] => 
        [26] => 
        [27] => 
        [28] => 
        [29] => 
        [30] => 
    )

Using trim followed by explode(" ",$String); or the regular expression posted above, returns a similar result, but with 20 keys instead of 30


using the answer posted, I have got the following:

[0] => Array
        (
            [0] => 
Running  ADWS               Active Directory Web Services         
        )

which is not as expected

4

2 回答 2

2

它适用于我使用preg_split和你的正则表达式/\s+/

<?php
$s = 'Running  ADWS               Active Directory Web Services   ';
var_dump(preg_split('/\s+/', $s));
var_dump(preg_split('/\s+/', trim($s)));

产生以下输出:

array(7) {
  [0]=>
  string(7) "Running"
  [1]=>
  string(4) "ADWS"
  [2]=>
  string(6) "Active"
  [3]=>
  string(9) "Directory"
  [4]=>
  string(3) "Web"
  [5]=>
  string(8) "Services"
  [6]=>
  string(0) ""
}
array(6) {
  [0]=>
  string(7) "Running"
  [1]=>
  string(4) "ADWS"
  [2]=>
  string(6) "Active"
  [3]=>
  string(9) "Directory"
  [4]=>
  string(3) "Web"
  [5]=>
  string(8) "Services"
}

键盘上的示例

更新

你提供的信息肯定有很大帮助。它由PowerShell生成的事实已经让我意识到一个可能的问题,并且您提供的链接也让我可以查看实际Services.txt文件,这进一步证明了我的想法:

Services.txt文件使用 UTF-16 编码。UTF-16 是一种多字节字符串格式,与 UTF-8 不兼容。因此,您utf8_encode将什么也不做,因为您根本没有查看 UTF-8 内容。相反,您需要查看 php多字节字符串(因为 PHP 不支持本机 unicode 字符串)。

为方便起见,最好的选择是将文本转换为单字节字符串,例如 UTF-8。您可以使用mb_convert_encoding. 因此,不要从文件中调用utf8_encode文本,而是这样做:

$File = mb_convert_encoding(file_get_contents('Services.txt'), 'utf-8', 'utf-16');

然后它应该工作。

于 2013-05-02T00:05:31.953 回答
0

http://php.net/manual/en/function.explode.php

$arr = explode ( "\t", $file[3] );

注意使用双引号,因为:

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double

如果字符串用双引号 (") 括起来,PHP 将为特殊字符解释更多转义序列:

\t 水平制表符(HT 或 ASCII 中的 0x09 (9))

于 2013-05-01T23:59:53.917 回答