Feeds CSV parser splitting cells by a custom delimiter.
<?php | |
define('MULTI_VALUE_CUSTOM_DELIMITER', '\#'); | |
/** | |
* Extends FeedsCSVParser to explode row cells by a custom delimiter. | |
* | |
* Cells that start with \# will be exploded by the delimiter \#. | |
* | |
* So a cell that reads: | |
* | |
* \#one\#two\#three | |
* | |
* will be exploded into an array with the values | |
* | |
* 'one' | |
* 'two' | |
* 'three' | |
*/ | |
class MultiValueCSVParser extends FeedsCSVParser { | |
/** | |
* Parse all of the items from the CSV. | |
*/ | |
protected function parseItems(ParserCSV $parser, ParserCSVIterator $iterator, $start = 0, $limit = 0) { | |
$rows = parent::parseItems($parser, $iterator, $start, $limit); | |
foreach ($rows as &$row) { | |
foreach ($row as &$cell) { | |
if ($cell && is_string($cell) && strpos($cell, '\#') === 0) { | |
$cell = substr($cell, 2); | |
$cell = explode(MULTI_VALUE_CUSTOM_DELIMITER, $cell); | |
$cell = count($cell) == 1 ? array_shift($cell) : $cell; | |
} | |
} | |
} | |
return $rows; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment