iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👌

Implementing ByteIterator in PHP

に公開

I wrote this for a change of pace. I might have an opportunity to use it for displaying HTTP/2 frame debug logs in the future.

$str = "あいうえお";
$iter = ByteIterator($str);

foreach ($iter as $value) {
    echo strtoupper(bin2hex($value)), ' ';
}

echo PHP_EOL;

function ByteIterator(string $str): Generator {
    $length = strlen($str);

    for ($i = 0; $i < $length; ++$i) {
        yield $str[$i];
    }

}

Discussion