PHP Doku:: Führt die ROT13-Transformation auf einen String aus - function.str-rot13.html

Verlauf / Chronik / History: (50) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzTextverarbeitungZeichenkettenString-Funktionenstr_rot13

Ein Service von Reinhard Neidl - Webprogrammierung.

String-Funktionen

<<str_replace

str_shuffle>>

str_rot13

(PHP 4 >= 4.2.0, PHP 5)

str_rot13Führt die ROT13-Transformation auf einen String aus

Beschreibung

string str_rot13 ( string $str )

Diese Funktion führt die ROT13-Transformation auf die Zeichenkette str aus und liefert den Ergebnisstring zurück.

Die ROT13-Kodierung verschiebt einfach jeden Buchstaben um 13 Stellen im Alphabet. Zeichen, die keine Buchstaben sind, bleiben unverändert. Verschlüsselung und Entschlüsselung werden von derselben Funktion durchgeführt. Bei Übergabe eines verschlüsselten Strings wird der Originalstring zurückgegeben.

Parameter-Liste

str

Die Eingabezeichenkette.

Rückgabewerte

Gibt die ROT13-Version der angegebenen Zeichenkette zurück.

Beispiele

Beispiel #1 str_rot13()-Beispiel

<?php

echo str_rot13('PHP 4.3.0'); // CUC 4.3.0

?>

Changelog

Version Beschreibung
4.3.0 Das Verhalten dieser Funktion wurde korrigiert. Vor der Korrektur wurde der str-Parameter selbst ebenfalls verändert, als wenn er als Reference übergeben worden wäre.


8 BenutzerBeiträge:
- Beiträge aktualisieren...
shaunspiller at spammenotgmail dot com
26.09.2009 17:54
Here's my implementation of a str_rot that takes a custom offset. It's faster than the others here because it allocates the output string in one go instead of tacking on characters one at a time. It can handle positive or negative offsets of any size, and it fixes everything up to ensure only upper and lower case letters are translated and that they wrap around correctly within the alphabet.

<?php
function str_rot($s, $n = 13) {
   
$n = (int)$n % 26;
    if (!
$n) return $s;
    for (
$i = 0, $l = strlen($s); $i < $l; $i++) {
       
$c = ord($s[$i]);
        if (
$c >= 97 && $c <= 122) {
           
$s[$i] = chr(($c - 71 + $n) % 26 + 97);
        } else if (
$c >= 65 && $c <= 90) {
           
$s[$i] = chr(($c - 39 + $n) % 26 + 65);
        }
    }
    return
$s;
}
?>
peter at NOSPAM jamit dot com
7.09.2009 16:31
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); // 中國是我的家

?>
peter at NOSPAM jamit dot com
7.09.2009 10:11
Here is my ROT13 function that works for all possible characters and not just ASCII. It can be used on Chinese, Japanese, ....

<?php

function rot13encrypt ($str) {
    return
str_rot13(base64_encode($str));
    }

function
rot13decrypt ($str) {
    return
base64_decode(str_rot13($str));
    }

// example

$string = '中國是我的家';

$encrypted = rot13encrypt ($string); // produces 5Yvg5MlY5cvi5bvE55dR5n62

$decrypted = rot13decrypt ($encrypted); // produces 中國是我的家

?>
grawity+phpnet at gmail dot com
31.12.2007 14:39
resubmitting updated function in re:#76975
<?php
function asc_shift($str,$offset=0) {
   
$new = '';
    for (
$i = 0; $i < strlen($str); $i++)
       
$new .= chr(ord($str[$i])+$offset);
    return
$new;
}
?>
because as of PHP 6, $str{$i} is deprecated.
nick at lazy-river dot net
8.08.2007 18:01
This is recursive function to shift the component letters of a string left or right in the ascii table.

I've left it simple as it suits my needs, but you may want to include error checking for a null string and also put bounds in place, or make it actually rotate around the whole character set rather than just shifting the string up or down.

function asc_shift($string, $amount) {
  $key = substr($string, 0, 1);
  if(strlen($string)==1) {
    return chr(ord($key) + $amount);
  } else {
    return chr(ord($key) + $amount) . asc_shift(substr($string, 1, strlen($string)-1), $amount);
  }
}

For example:
<?php
echo asc_shift("TESTING12345@", 5);
?>

shifts every character up 5 ascii positions, resulting in this string:

YJXYNSL6789:E

In reverse:

<?php
echo asc_shift("YJXYNSL6789:E", -5);
?>

shifts every character down 5 ascii positions, resulting in this string:

TESTING12345@
arwab at surrealwebs dot com
11.07.2007 22:11
here's my rot function, it works anyway
<?php
/**
 * preforms the rotation algorithm on the passed in string
 */
function _rot( $str , $dist=13 ){
    if( !
is_numeric($dist) || $dist < 0){
       
$dist = 13;
    }

   
$u_lower 65; $u_upper 90;
   
$l_lower 97; $l_upper = 122;
   
   
$char_count = ($u_upper - $u_lower) +1;

    while(
$dist > $char_count ){
       
$dist -= $char_count;
    }

   
$newstr = '';
   
    for(
$i=0; $i<strlen($str); ++$i){
       
$c = ord($str[$i]);

       
/*
         * Check if the character is within the bounds of our function (a-zA-z)
         * if not it gets tacked on to the string as is and we move on to the
         * next one.
         */
       
if( $c<$u_lower || $c>$l_upper || ( $c>$u_upper && $c <$l_lower ) ){
           
$newstr .= chr($c);
            continue;
        }

       
$lower = ( $c<=$u_upper?$u_lower:$l_lower);
       
$upper = ( $c<=$u_upper?$u_upper:$l_upper);

       
$c += $dist;

        if(
$c > $upper){
           
$c = (($c - $upper) + ($lower-1));
        }

       
$newstr .= chr($c);
    }
   
    return
$newstr;
}
?>
electro at whatever dot com
31.05.2007 21:21
<?php

/**
 * Rotate each string characters by n positions in ASCII table
 * To encode use positive n, to decode - negative.
 * With n = 13 (ROT13), encode and decode n can be positive.
 *
 * @param string $string
 * @param integer $n
 * @return string
 */
function rotate($string, $n) {
   
   
$length = strlen($string);
   
$result = '';
   
    for(
$i = 0; $i < $length; $i++) {
       
$ascii = ord($string{$i});
       
       
$rotated = $ascii;
       
        if (
$ascii > 64 && $ascii < 91) {
           
$rotated += $n;
           
$rotated > 90 && $rotated += -90 + 64;
           
$rotated < 65 && $rotated += -64 + 90;
        } elseif (
$ascii > 96 && $ascii < 123) {
           
$rotated += $n;
           
$rotated > 122 && $rotated += -122 + 96;
           
$rotated < 97 && $rotated += -96 + 122;
        }
       
       
$result .= chr($rotated);
    }
   
    return
$result;
}

$enc = rotate('string', 6);
echo
"Encoded: $enc<br/>\n";
echo
'Decoded: ' . rotate($enc, -6);

?>
maximius at gmail dot com
26.05.2007 1:30
Perhaps someone will find this useful ;)

<?
          
function rotN($s, $n){
               
$s2 = "";
                for(
$i = 0; $i < strlen($s); $i++){
                   
$char2 = $char = ord($s{$i});
                   
$cap = $char & 32;

                   
$char &= ~ $cap;
                   
$char = $char > 64 && $char < 123 ? (($char - 65 + $n) % 26 + 65) : $char;
                   
$char |= $cap;
                    if(
$char < 65 && $char2 > 64 || ($char > 90 && $char < 97 && ($char2 < 91 || $char2 > 96))) $char += 26;
                    else if(
$char > 122 && $char2 < 123) $char -= 52;
                    if(
strtoupper(chr($char2)) === chr($char2)) $char = strtoupper(chr($char)); else $char = strtolower(chr($char));
                   
$s2 .= $char;
                }
                return
$s2;
            }
?>
It takes any string, $s, and any ROT value, $n. Just like str_rot13, it's both an encoder and decoder. To decode an encoded string, just pass -$n instead of $n.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)