Skip to content

Instantly share code, notes, and snippets.

@morontt
Created April 16, 2020 19:09
Show Gist options
  • Save morontt/2c49983a946aa16ce7ff9a7fbfdef231 to your computer and use it in GitHub Desktop.
Save morontt/2c49983a946aa16ce7ff9a7fbfdef231 to your computer and use it in GitHub Desktop.
<?php
/**
* Результат:
* Тестовый текст с тегом 1,
* Итак, счётчик 2, 3 - 4
* И ещё 5
* 6
*/
$text = <<<TEXT
Тестовый текст с тегом [iterator],
Итак, счётчик [iterator], [iterator] - [iterator]
И ещё [iterator]
[iterator]
TEXT;
$generator = (function () {
$i = 0;
while (true) {
$i++;
yield $i;
}
})();
$text = preg_replace_callback(
'/\[iterator\]/i',
function ($matches) use ($generator) {
$cnt = $generator->current();
$generator->next();
return $cnt;
},
$text
);
echo $text . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment