Skip to content

Instantly share code, notes, and snippets.

@donatj
Last active Oct 15, 2015
Embed
What would you like to do?
Gets byte length of characters read from stdin
#!/usr/bin/env php
<?php
mb_internal_encoding('UTF-8');
$string = '';
if( count($argv) > 1 ) {
$string .= implode(' ', array_slice($argv, 1));
} else {
while( $line = fgets(STDIN) ) {
$string .= $line;
}
$string = substr($string, 0, -1);
}
$len = mb_strlen($string);
for( $i = 0; $i < $len; $i++ ) {
$rune = mb_substr($string, $i, 1);
echo " rune: " . $rune . "\n";
echo "byte length: " . strlen($rune) . "\n";
echo " mb length: " . mb_strlen($rune) . "\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment