Last active
October 15, 2015 20:58
-
-
Save donatj/a5cfe4c30a52f935f4b9 to your computer and use it in GitHub Desktop.
Gets byte length of characters read from stdin
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
#!/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