<?php function to_hex($matches) { return sprintf(' %02X', ord($matches[1])); } echo preg_replace_callback("/(.)/s", "to_hex", "!\"#$%&'()"); ?>
"!\"\#$%&'()".gsub(/./){|c| " %02X" % c.ord}