note 23816 deleted from function.dechex by dams
| From: | dams@php.net | Date: | Tue, 30 Jul 2002 00:35:09 +0000 |
| Subject: | note 23816 deleted from function.dechex by dams | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-33927@lists.php.net to get a copy of this message | ||
I use the dechex function in a simple little encoder/decoder I use to let me convert binaries to
safe strings (for giving to programs outside PHP) but I mod the value against 16 to force a 2
character hex value return.
e.g. CRLF -> "%0d%0a"
function stringEncode($inString) { // Encodes string
return (string) preg_replace ("/([^a-zA-Z0-9])/e",
"'%'.dechex(ord(substr('$1', -1, 1))/16).dechex(ord(substr('$1',
-1, 1))%16)", $inString);
}
function stringDecode($inString) { // Decodes string
return (string) preg_replace ("/%([a-fA-F0-9][a-fA-F0-9])/e",
"chr(hexdec('$1'))", $inString);
}