Zend - The PHP Company




Miscellaneous

Add Code


caesar shift  

Type: code fragment
Added by: robF
Entered: 03/11/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 5504
Totally useless script to caesar shift a given message.


<?php

/*=============================================================================
 *
 * caeser_shift.php
 *
 * Robert Fisher 11/2000
 *
 * Just got sent a stupid little message which looked like it was ROT13-ed,
 * but it wasn't.  It looked like a Caesar shift cypher, so I hacked
 * together a little script to decode it. It's really rubbish - it mangles
 * case, removes spaces completely, and it simply /has/ to be the
 * most useless script on this site. Hurrah!
 *
 *==========================================================================*/

$msg "insert your message here";

/* this loop prints all 25 caesar shifts of the message */

for ($offset 0$offset 26$offset++) {
    echo 
"<P> shift of $offset : "rot($msg$offset);
}


function 
get_no($letter$alphabet) {

    
// crudely return the position in the alphabet of the letter we're given

    
if (!ereg("^[a-zA-Z]$"$letter))
        return 
false;

    for (
$i 0$i sizeof($alphabet); $i++) {

        if (
strtolower($letter) == $alphabet[$i])
            break;
            
    }

    return 
$i;

}

function 
rot($msg$offset) {

    
$from $to = array();
    
$msg explode(" "trim(chunk_split($msg1" ")));

    
/* generate an alphabet */

    
for ($i ord("a"); $i <= ord("z"); $i++) {
        
$from[] = chr($i);
    }

    
/* join two alphabets together so we can do the Caesar shift */

    
$to array_merge($from$from);

    
/* go through each letter, building up an array containing the shifted
     * message */

    
for ($i 0$i sizeof($msg); $i++) {

        if (
$shifted get_no($msg[$i], $from))
            
$out[] = $to[$shifted $offset];
    
    }
    
    
/* squash the shifted message into a string and return it */
    
    
return implode(""$out);

}

?>


Usage Example




Rate This Script





Search



This Category All Categories