PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

str_shuffle> <str_replace
Last updated: Fri, 10 Jul 2009

view this page in

str_rot13

(PHP 4 >= 4.2.0, PHP 5)

str_rot13Effectue une transformation ROT13

Description

string str_rot13 ( string $str )

Effectue un encodage ROT13 de la chaîne str et retourne le résultat.

L'encodage ROT13 décale toutes les lettres de 13 dans l'alphabet, et laisse tous les autres caractères inchangés. L'encodage et le décodage est fait par la même fonction : passer le résultat de str_rot13() à nouveau comme argument, retournera la chaîne originale.

Liste de paramètres

str

La chaîne d'entrée.

Valeurs de retour

Retourne la version ROT13 de la chaîne fournie.

Exemples

Exemple #1 Exemple avec str_rot13()

<?php

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

?>

Historique

Version Description
4.3.0 Le comportement de cette fonction a été corrigé. Avant cela, le paramètre str était aussi modifié, comme si il avait été passé par référence.



str_shuffle> <str_replace
Last updated: Fri, 10 Jul 2009
 
add a note add a note User Contributed Notes
str_rot13
grawity+phpnet at gmail dot com
31-Dec-2007 01: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
08-Aug-2007 04: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-Jul-2007 08: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-May-2007 07: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
25-May-2007 11: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.

str_shuffle> <str_replace
Last updated: Fri, 10 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites