I was having the same problem. The second way was what I was looking for.

Thank you so much for your help--I did not know about preg_match_all().

very cool....thanks again.

-Andres


Shawn McKenzie wrote:
Shawn McKenzie wrote:
Well in your approach you get a bunch of empty elements where the spaces
are.  Here are two ways but I'm sure one preg_match_all() without the
explodes and loop could do it (some guru will show us):

//one way
$text = 'xxx  xxxx  xx  xxxxx  xx   xxx
xx  xxxxx   x  xxx  xx  xxxxxx
xx  xxx  xx  xxxx  xx   xx';

$lines = explode(PHP_EOL, $text);

foreach($lines as $line) {
        $temp = explode(' ', $line);
        $result[] = array_filter($temp, 'reduce');
}
function reduce($var) {
        return !empty($var);
}
print_r($result);
Array
(
    [0] => Array
        (
            [0] => xxx
            [2] => xxxx
            [4] => xx
            [6] => xxxxx
            [8] => xx
            [11] => xxx
        )

    [1] => Array
        (
            [0] => xx
            [2] => xxxxx
            [5] => x
            [7] => xxx
            [9] => xx
            [11] => xxxxxx
        )

    [2] => Array
        (
            [0] => xx
            [2] => xxx
            [4] => xx
            [6] => xxxx
            [8] => xx
            [11] => xx
        )

)
//another way
$text = 'xxx  xxxx  xx  xxxxx  xx   xxx
xx  xxxxx   x  xxx  xx  xxxxxx
xx  xxx  xx  xxxx  xx   xx';

$lines = explode(PHP_EOL, $text);

foreach($lines as $line) {
        preg_match_all('|([^\s]+)+|', $line, $matches);
        $result[] = $matches[1];
}
print_r($result);
Array
(
    [0] => Array
        (
            [0] => xxx
            [1] => xxxx
            [2] => xx
            [3] => xxxxx
            [4] => xx
            [5] => xxx
        )

    [1] => Array
        (
            [0] => xx
            [1] => xxxxx
            [2] => x
            [3] => xxx
            [4] => xx
            [5] => xxxxxx
        )

    [2] => Array
        (
            [0] => xx
            [1] => xxx
            [2] => xx
            [3] => xxxx
            [4] => xx
            [5] => xx
        )

)

There is a difference in the key numbering though if that is important.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to