tags:

views:

51

answers:

2

Hi,

I have an array which is being built using:

$features_data[$i] = preg_split("/\n/", $row);

The output is as follows:

Array
(
    [0] => Array
        (
            [0] =>   
            [1] =>   
            [2] =>                 <img src="http://example.com/sites/test/files/imagecache/feature-image/testimage_0.jpg" alt="" title=""  class="imagecache imagecache-feature-image imagecache-default imagecache-feature-image_default" width="654" height="260" />      
            [3] => 
            [4] =>   
            [5] =>   
            [6] =>                 Test 1      
            [7] => 
            [8] =>   
            [9] =>   
            [10] =>                 Lorem ipsum dolor sit...      
            [11] => 
            [12] => 
        )

    [1] => Array
        (
            [0] =>   
            [1] =>   
            [2] =>                       
            [3] => 
            [4] =>   
            [5] =>   
            [6] =>                 Test 2      
            [7] => 
            [8] =>   
            [9] =>   
            [10] =>                 Aenean id tellus nec...      
            [11] => 
            [12] => 
        )

    [2] => Array
        (
            [0] =>   
            [1] =>   
            [2] =>                       
            [3] => 
            [4] =>   
            [5] =>   
            [6] =>                 Test 3      
            [7] => 
            [8] =>   
            [9] =>   
            [10] =>                 Maecenas ut pharetra...      
            [11] => 
            [12] => 
        )

)

I'd like to get rid of the blank array items and then reset the array counter. I've tried using php unset but for some reason it's not working. Any help would be greatly appreciated.

Thanks

+3  A: 

You could use array_filter() but what you really want to use is PREG_SPLIT_NO_EMPTY instead.

preg_split("/\n/", $row, -1, PREG_SPLIT_NO_EMPTY)

Edit: of course it entirely depends on your output. Your pattern should probably be something like this

preg_split("/[\\n\\r \\t]+/", $row, -1, PREG_SPLIT_NO_EMPTY)

or this

preg_split("/[\\n\\r \\t]*\\n[\\n\\r \\t]*/", $row, -1, PREG_SPLIT_NO_EMPTY)
Josh Davis
Thanks, the sub array items are now down to 9 items but there are still blanks which is really odd.
digital
@digital could it be you are on windows and your linebreak isn't `\n`? Try `'/'.PHP_EOL.'/'` for pattern.
Gordon
or you could split using `/\r|\n/` ? Then both line-endings should work, and PREG_SPLIT_NO_EMPTY will remove anything left
Tom Haigh
Use var_dump() rather than print_r() to inspect the return. You'll probably see that the elemnents you thought were empty were actually not and they contain spaces or other whitespace characters. I have posted an updated pattern.
Josh Davis
@Josh Davis, your correct var_dump is showing that there are two blank spaces per line. The code you added splits every word into a seperate item, getting there but regex really isn't my thing, I really appreciate all your help.
digital
Now that it is clear that your problem is the regexp rather than filtering the results, you should open a new question devoted to that topic, explaining exactly *how* you want to split that text, along with a sample text.
Josh Davis
A: 

I understand you want to do this:

$array = array(null, null, 'Test', null, null);
$output = array_values(array_filter($array));
var_dump($output);

outputs:

array(1) {
  [0]=>
  string(4) "Test"
}

When no callback is passed to array_filter, it will only return values not evaluating to false. Values will be typecasted, so 0 is false, as is '' or null. Keep in mind though that the resulting array from your preg_split might contain control chars. If the resulting array var_dumps not as given above, try to change the pattern in preg_split to '/'.PHP_EOL.'/'.

Alson note that you either want to use the single preg_split with flags (as given in another answer) or array_values plus array_filter plus explode for performance reasons. Using array_values plus array_filter plus preg_split is 2 times slower than either of the other two alternatives, which approximately perform the same.


$str = <<< TXT

Test

Foo

Bar

TXT;

Testing with

$start = microtime(true);
for($i=0; $i<100000; $i++) {
    array_values(array_filter(explode(PHP_EOL, $str)));
}
echo microtime(true) - $start, PHP_EOL; // 0.60249495506287

$start = microtime(true);
for($i=0; $i<100000; $i++) {
    array_values(array_filter(preg_split("/".PHP_EOL."/", $str)));
}
echo microtime(true) - $start, PHP_EOL; // 1.0394451618195

$start = microtime(true);
for($i=0; $i<100000; $i++) {
    preg_split("/".PHP_EOL."/", $str, -1, PREG_SPLIT_NO_EMPTY);
}
echo microtime(true) - $start, PHP_EOL; // 0.60252904891968
Gordon