note 44912 added to function.bin2hex

From: Date: Thu, 19 Aug 2004 22:05:07 +0000
Subject: note 44912 added to function.bin2hex
Groups: php.notes 
Request: Send a blank email to php-notes+get-74959@lists.php.net to get a copy of this message
Here's a modified version of an earlier post (asc2bin and bin2asc) to convert an incoming ascii string to hex and out again. For example, this is really useful if you want to insert data into a mySQL database which contains both escaped and non-escaped characters. For example, if you want to store code snibbets in a mySQL text field: printf("here's some \"text\"."); If you INSERT this into mySQL, it probably won't come out in friendly executable format, and you can't escape all double-quotes, nor can you strip out all the slashes. One solution is to just convert the string to hex, store it in hex, then convert back to ascii before using it in some way: function asc2hex ($temp) { $len = strlen($temp); for ($i=0; $i<$len; $i++) $data.=sprintf("%02x",ord(substr($temp,$i,1))); return $data; } function hex2asc($temp) { $len = strlen($temp); for ($i=0;$i<$len;$i+=2) $data.=chr(hexdec(substr($temp,$i,2))); return $data; } ---- Manual Page -- http://www.php.net/manual/en/function.bin2hex.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44912 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44912&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44912&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74959) next »