0

I am trying to split the line have space and text enclosed.. I am not getting proper output

$line = '"name" "Brand" "EAN"   "shipping" "Combinable"';
$lineArray = preg_split('/^\"\s+\"/', $line);
print_r($lineArray);
nickb
  • 59,313
  • 13
  • 108
  • 143
B L Praveen
  • 1,812
  • 4
  • 35
  • 60

1 Answers1

1

You don't need to escape the quotes, and your split is incorrect anchored at the beginning of the string. Try this:

$line = '"name" "Brand" "EAN"   "shipping" "Combinable"';
$lineArray = preg_split('/"\s+"/', $line);

Note that this causes the first element to start with a quote, and the last element to end with a quote. To keep the quotes, use assertions:

$lineArray = preg_split('/(?<=")\s+(?=")/', $line);

This produces a $lineArray like (note how it kept the quotes):

Array ( [0] => "name" [1] => "Brand" [2] => "EAN" [3] => "shipping" [4] => "Combinable" )
nickb
  • 59,313
  • 13
  • 108
  • 143