Skip to content

Instantly share code, notes, and snippets.

@donatj
Last active October 15, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donatj/a5cfe4c30a52f935f4b9 to your computer and use it in GitHub Desktop.
Save donatj/a5cfe4c30a52f935f4b9 to your computer and use it in GitHub Desktop.
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