I think this will do it, but I'll leave it to you to test against your Perl script, to see if the encodings are the same. (I went OOP just to keep things straight in my mind, rather than try to imitate the terseness of Perl. 🙂 )
<?php
class Obfuscator
{
/**
* @var array Transliteration array
*/
private $trans = array();
/**
* Constructor
* @return void
*/
public function __construct()
{
// build the transliteration array
$orig = array_merge(range('a', 'z'), range('A', 'Z'));
$trans = array_merge(
range('s', 'z'),
range('a', 'r'),
range('S', 'Z'),
range('A', 'R')
);
$this->trans = array_combine($orig, $trans);
}
/**
* Encode a string
* @return string
* @param string $str
*/
public function encode($str)
{
$result = '';
for($i=0, $len=strlen($str); $i<$len; $i++) {
$result .= sprintf(
'%02x',
ord((isset($this->trans[$str[$i]])) ? $this->trans[$str[$i]] : $str[$i])
);
}
return $result;
}
/**
* Decode a string
* @return string
* @param string $str
*/
public function decode($str)
{
$result = '';
for($i=0,$len=strlen($str); $i<$len; $i+=2){
$char = chr(hexdec(substr($str, $i, 2)));
$result .= (in_array($char, $this->trans)) ?
array_search($char, $this->trans) : $char;
}
return $result;
}
}
// TEST:
$test = new Obfuscator();
$encoded = $test->encode('Nog@Dog.com');
$decoded = $test->decode($encoded);
echo "<pre>$encoded
$decoded</pre>";