Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

It works for me using preg_split and your regular expression /s+/:

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

Yields the following output:

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"
}

Example on codepad

Update

The information you provided definitely helped a lot. The fact that it is generated by PowerShell already made me realize a possible problem, and the link you provided also allowed me to take a look at the actual Services.txt file, which further proved my idea:

The Services.txt file is encoded with UTF-16. UTF-16 is a multibyte string format and not compatible with UTF-8. So your utf8_encode will do nothing because you are not looking at UTF-8 content at all. Instead, you need to look at the php multibyte strings (because PHP supports no native unicode strings).

To make it easy, the best option would be to just convert your text to a single byte string, e.g. UTF-8. You can do that using mb_convert_encoding. So instead of calling utf8_encode on the text from the file, just do this instead:

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

And then it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...