Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Created September 8, 2016 11:02
Show Gist options
  • Save littlefuntik/6fba3517319f0e51f1a8c2ef1d2eac79 to your computer and use it in GitHub Desktop.
Save littlefuntik/6fba3517319f0e51f1a8c2ef1d2eac79 to your computer and use it in GitHub Desktop.
example usage
<?php
$keywords = 'aa,bb,cc';
function splitGenerator($haystack, $separator = ',')
{
$separatorPos1 = -1;
while (true) {
$separatorPos2 = mb_strpos($haystack, $separator, $separatorPos1 + 1);
if (false === $separatorPos2) {
$chunk = mb_substr($haystack, - (mb_strlen($haystack) - $separatorPos1 - 1));
yield $chunk;
break;
}
$chunk = mb_substr($haystack, $separatorPos1 + 1, $separatorPos2 - $separatorPos1 - 1);
$separatorPos1 = $separatorPos2;
yield $chunk;
}
}
foreach (splitGenerator($keywords) as $keyword) {
echo $keyword . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment