Last active
February 4, 2016 09:28
-
-
Save carlosdagos/89f67808e01f97e6de91 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
define('DEBUG', isset($argv[2]) && strtolower($argv[2]) == '--debug'); // just set the last one to "--debug" | |
$locale = explode('.', getenv('LC_ALL'))[1]; | |
echo sprintf('System locale: %s [mb_internal_encoding set to: %s]' . "\n", getenv('LC_ALL'), $locale); | |
function rotateStringSimple($str) { | |
$res = ''; | |
if (DEBUG) echo sprintf('%s String length: %s' . "\n", __FUNCTION__, strlen($str)); | |
for ($i = 0; $i < strlen($str); $i++) { | |
$chr = $str{$i}; | |
$res = $chr . $res; | |
if (DEBUG) { | |
echo sprintf('%s: Rotated hex value: %s [%s]' . "\n", __FUNCTION__, bin2hex($chr), $chr); | |
} | |
} | |
return $res; | |
} | |
function rotateStringMb($str) { | |
$res = ''; | |
if (DEBUG) echo sprintf('%s String length: %s' . "\n", __FUNCTION__, mb_strlen($str)); | |
for ($i = 0; $i < mb_strlen($str); $i++) { | |
$chr = mb_substr($str, $i, 1); | |
$res = $chr . $res; | |
if (DEBUG) { | |
echo sprintf('%s: Rotated hex value: %s [%s]' . "\n", __FUNCTION__, bin2hex($chr), $chr); | |
} | |
} | |
return $res; | |
} | |
$str = $argv[1]; // shell call: $ ~ php script.php argument -- "argument" is in the 2nd pos | |
echo sprintf('Your string reversed (sb): %s' . "\n", rotateStringSimple($str)); | |
echo sprintf('Your string reversed (mb): %s' . "\n", rotateStringMb($str)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
php string_rotate_test.php 水之上