-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

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • Are these actually tabs, or a series of spaces? It's an important distinction. Your question title and wording suggests they're tabs, but your comments against answers suggest otherwise. – Sepster May 02 '13 at 00:10
  • @Sepster As said within your answer " I can see the confusion, I considered this to be a tab space, if you can think of a better title name, then by all means edit. I do not mean to cause confusion lmfao" – Daryl Gill May 02 '13 at 00:12
  • Have you tried using [Powershell with the Export-CSV commandlet](http://technet.microsoft.com/en-us/library/hh849932.aspx)? That would make the content more accessible. You can easily [read a CSV file with PHP](http://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php/2805486#2805486). – Gordon May 02 '13 at 11:46

2 Answers2

2

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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Using the regular expression returns what I have posted. I will setup a few pastebins one for the text file being read, and then the other for my entire code. – Daryl Gill May 02 '13 at 00:07
  • @DarylGill Hmm, it still works fine for me. What PHP version are you using? – poke May 02 '13 at 00:14
  • PHP Version 5.4.11.. I can link you to the website I am running? so you can see the results – Daryl Gill May 02 '13 at 00:17
  • @DarylGill I’m using PHP 5.4.14 (so only unimportant minor differences that shouldn’t matter) and it works here. Maybe it’s some issue with how the source file is originally stored (encoding and such). A link would be helpful. – poke May 02 '13 at 00:19
  • http://www.slayer-productions.com/ServiceMonitor/GetAll.php -- This is where it's running from, if it's any assistance, the text file is generated from a powershell script (.ps1) to direct the outputs into the working folder, which is then read.. I encode to UTF-8, because if I do not.. I get chinese symbols when printing out the exploded contents – Daryl Gill May 02 '13 at 00:20
  • if you was here. I would give you a hug right about now, I have been pulling my hair out over this problem! Now i've got rid of all my unnecessary keys within the array, the pattern is that $File[0] is the status and $File[1] is the Service name.. extract them two from the array, unset.. Implode the rest then add to an array.. Many thanks! – Daryl Gill May 02 '13 at 00:41
  • If you wish to see the finished result: http://Slayer-productions.com/ServiceMonitor/GetAll.php this was done with your assistance! – Daryl Gill May 02 '13 at 00:51
0

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

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

Note the use of double quotes, because:

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

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

\t horizontal tab (HT or 0x09 (9) in ASCII)

Sepster
  • 4,800
  • 20
  • 38
  • It’s not tab-separated, there are actual spaces. – poke May 02 '13 at 00:06
  • @poke Ah, I get it. The OP's question title "_Explode at **tab** spaces?_" and wording "_How would I explode at **tab** space...?_" so not sure why I misunderstood ;-) – Sepster May 02 '13 at 00:07
  • I can see the confusion, I considered this to be a tab space, if you can think of a better title name, then by all means edit. I do not mean to cause confusion lmfao – Daryl Gill May 02 '13 at 00:10