Created
April 16, 2020 19:09
-
-
Save morontt/2c49983a946aa16ce7ff9a7fbfdef231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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