-1

I have a string called $output which returns a list like this

    Object One


    Object Two


    Object Three

    Object Four
    Object Five

    Object Six

I want to turn this into an array using explode like this:

explode("\n", $output);

However, there are lots of blank lines and random white spaces in the beginning. Is there a way to remove all of this so that I am left with a usable array?

TarangP
  • 2,711
  • 5
  • 20
  • 41
Ahmed
  • 1,403
  • 4
  • 21
  • 44
  • 3
    Possible duplicate of [How to trim white spaces of array values in php](https://stackoverflow.com/questions/5762439/how-to-trim-white-spaces-of-array-values-in-php) – CBroe Jan 04 '18 at 10:39
  • 2
    You can [`trim()`](http://php.net/manual/en/function.trim.php) each line produced by [`explode()`](http://php.net/manual/en/function.empty.php) and ignore those that are [empty strings](http://php.net/manual/en/function.strlen.php) after the trim. Or you can use [`preg_split()`](http://php.net/manual/en/function.preg-split.php) to use as delimiter any sequence of white spaces that includes at least one newline. – axiac Jan 04 '18 at 10:39

5 Answers5

2

You can use array_filter to filter out the empty strings from the array

array_filter(explode("\n", $output));

For removing the whitespaces in the beginning, use array_map

$result = array_filter(explode("\n", $output));
array_map('trim', $result);
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • Should use PHP_EOL instead of "\n" for cross platform compatability and you need to store the array_map result i.e. $result = array_map( 'trim', $result); –  Jan 04 '18 at 10:54
1

You can trim() each line produced by explode() and ignore those that are empty strings after the trim.

Or you can use preg_split() to use as delimiter any sequence of white spaces that includes at least one newline:

$text = <<<E
        Object One


        Object Two


        Object Three

        Object Four
               Object Five

        Object Six
E;

var_dump(preg_split('/\s*\n(\s*\n)*\s*/', trim($text)));

The output is:

array(6) {
  [0]=>
  string(10) "Object One"
  [1]=>
  string(10) "Object Two"
  [2]=>
  string(12) "Object Three"
  [3]=>
  string(11) "Object Four"
  [4]=>
  string(11) "Object Five"
  [5]=>
  string(10) "Object Six"
}

The regex:

\s*            # zero or more (*) whitespaces (\s)
\n             # a newline character
(              # beginning of a group
    \s*\n      # zero or more whitespaces followed by a newline
)              # end of the group
*              # zero or more repetitions of the previous expression (the group)
\s*            # zero of more whitespaces
axiac
  • 68,258
  • 9
  • 99
  • 134
1

Use preg_split() to split on all newline sequences (\R) and include zero or more leading or trailing whitespace sequences (\s*). Pre-trim the input in case there are whitespaces at the start or end of the input string that do not neighbor a newline sequence.

Code: (Demo)

var_export(preg_split('~\s*\R\s*~', trim($string), -1, PREG_SPLIT_NO_EMPTY));

Output:

array (
  0 => 'Object One',
  1 => 'Object Two',
  2 => 'Object Three',
  3 => 'Object Four',
  4 => 'Object Five',
  5 => 'Object Six',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
 <?php 
 $data = " Object One


    Object Two


    Object Three

    Object Four
           Object Five

    Object Six";

    $data = explode("\n", $data);
    $finalArray = array();
    foreach($data as $value){
         if(!empty(trim($value))){
            $finalArray[] = trim($value);  
         }
    }

    print_r($finalArray);

output:

Array ( [0] => Object One [1] => Object Two [2] => Object Three [3] => Object Four [4] => Object Five [5] => Object Six )

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
0

Try this...

Input

 $data = "        Object One


        Object Two


        Object Three

        Object Four
               Object Five

        Object Six";

Solution

$result = array_filter(array_map('trim',explode("\n", $data)));
echo "<pre>";   print_r($result);

Output

Array
(
    [0] => Object One
    [3] => Object Two
    [6] => Object Three
    [8] => Object Four
    [9] => Object Five
    [11] => Object Six
)
GYaN
  • 2,327
  • 4
  • 19
  • 39