Forum Moderators: coopster

Message Too Old, No Replies

&#nnn; thingies and $ POST data

         

ryeterrell

5:35 am on Aug 23, 2006 (gmt 0)

10+ Year Member



I was having some trouble adding strings to my mysql database when it had things like question marks in it, etc, so I made this function to convert every character of the string into whatever this format is called: &#nnn; where nnn is the ascii code of the character I am encoding. This is the function:


function CleanString($string)
{
$out = "";
$length = strlen($string);
for($i = 0; $i < $length; $i++)
$out = $out . "&#" . ord(substr($string, $i, 1)) . ";";
return $out;
}

The function works great. The problem I am having is when I have something like:


<input type="checkbox" name="<?PHP echo $opt;?>">

Where $opt is a string encoded into the &#nnn; format. For some reason, I can only seem to access it as the unencoded string through the $_POST array. i.e., if the encoded string is, say, '88', then I can use $_POST['88']. I can't use the encoded version of '88' like $_POST['&#57;&#57;']. And I would really like to be able to.

Is there some solution? I have tried different quotes, etc.

Btw, I am trying to acces it as $_POST[$opt], where $opt is the encoded version of the string.

Thanks

Rye

willybfriendly

7:40 am on Aug 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Those "thingies" are called HTML entities. Have you tried htmlentities [us2.php.net]?

WBF

ryeterrell

1:38 pm on Aug 23, 2006 (gmt 0)

10+ Year Member



Yes, but it leaves things like "\" in whatever string I need to be safe for browser and database.

Anyway, I finally got frustrated with using entities on every char and have since started using htmlentities. The problem now, of course, is those special characters not taken care of by htmlentities.

I hate that my code must be peppered by a bunch of snippets of stripping code. :P It makes everything so messy.

Rye

willybfriendly

2:36 pm on Aug 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are already functions for most every common task

$string = "They're a Lab\Shepard mix & only cost $25 each";

$string = addslashes(htmlentities($string));
echo $string;

output = They\'re a Lab\\Shepard mix &amp; only cost $25 each

You don't mention what db you are using, but assuming MySQL, better to use mysql_real_escape_string() than addslashes()

WBF