note 93388 added to function.str-rot13
| From: | peteratNOSPAMjamitdotcom at osu1 dot php dot net | Date: | Mon, 07 Sep 2009 14:31:28 +0000 |
| Subject: | note 93388 added to function.str-rot13 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160279@lists.php.net to get a copy of this message | ||
This ROT13 variant is different from my earlier version in that it retains 'ethnicity'.
For example, a Chinese text when encrypted will remain Chinese, and the string will not be making
sense (the real meaning will be encrypted). Just look at the code and you will understand.
<?php
function unichar2ords($char, $encoding = 'UTF-8') {
$char = mb_convert_encoding($char, 'UCS-4', $encoding);
$val = unpack('N', $char);
return $val[1];
}
function ords2unichar($ords, $encoding = 'UTF-8'){
$char = pack('N', $ords);
return mb_convert_encoding($char, $encoding, 'UCS-4');
}
function mbStringToArray ($string, $encoding = 'UTF-8') {
if (empty($string)) return false;
for ($strlen = mb_strlen($string, $encoding); $strlen > 0; ) {
$array[] = mb_substr($string, 0, 1, $encoding);
$string = mb_substr($string, 1, $strlen, $encoding);
$strlen = $strlen - 1;
}
return $array;
}
function unicodeRotN($str, $offset, $encoding = 'UTF-8') {
$val = '';
$array = mbStringToArray ($str, $encoding = 'UTF-8');
$len = count($array);
for ($i = 0; $i < $len; $i++) {
$val .= ords2unichar(unichar2ords($array[$i], $encoding) + $offset, $encoding);
}
return $val;
}
// example
$original = 'ä¸åæ¯æçå®¶'; // means "China is my home"
$encrypted = unicodeRotN($string, 13); // ä¸ºåæ¼æçå¯ means "à Ai
injustice for the Mission Day" (Google translation)
$decrypted = unicodeRotN($encrypted, -13); // ä¸åæ¯æçå®¶
?>
----
Server IP: 208.69.120.35
Probable Submitter: 210.209.78.56
----
Manual Page -- http://www.php.net/manual/en/function.str-rot13.php
Edit -- https://master.php.net/note/edit/93388
Del: integrated -- https://master.php.net/note/delete/93388/integrated
Del: useless -- https://master.php.net/note/delete/93388/useless
Del: bad code -- https://master.php.net/note/delete/93388/bad+code
Del: spam -- https://master.php.net/note/delete/93388/spam
Del: non-english -- https://master.php.net/note/delete/93388/non-english
Del: in docs -- https://master.php.net/note/delete/93388/in+docs
Del: other reasons-- https://master.php.net/note/delete/93388
Reject -- https://master.php.net/note/reject/93388
Search -- https://master.php.net/manage/user-notes.php