String En/Decoding

It took me a while to figure this one out, but here is a nice way to use preg_replace to convert a hex encoded string back to clear text

<?php
$text
= "PHP rocks!";
$encoded = preg_replace("'(.)'e","dechex(ord('\\1'))",$text);
print
"ENCODED: $encoded\n";
?>

results in ENCODED: 50485020726f636b7321

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

results in DECODED: PHP rocks!