Tuesday, January 4, 2011

PHP encode and decode hex

SkyHi @ Tuesday, January 04, 2011
<?php

$text = "php rocks!";
$encoded = preg_replace(
    "'(.)'e"
    ,"dechex(ord('\\1'))"
    ,$text
);
echo "ENCODED: $encoded\n";


//ENCODED: 50485020726f636b7321


echo "DECODED: ".preg_replace(
    "'([\S,\d]{2})'e"
    ,"chr(hexdec('\\1'))"
    ,$encoded)."\n";


//DECODED: php rocks!



?>