Working with '\'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Working with '\'

Post by pickle »

Hi all.

Ok, here's my issue. I've got a challenge tracking system that, obviously, people can put challenges into. The problem I'm having is with keeping any ''' characters they put in. Since I'm storing this info in MySQL, I put in slashes before any database non-friendly characters, like ' and ". How do I go about removing the slashes that addcslashes puts in, but not the ones the user puts in. To date I've tried:

- calling htmlentities() on the incoming text before it's put in the database, to hopefully convert user entered ''' to their html code equivalent.
- calling stripcslashes() on the outgoing text which I had thought would only strip slashes that came before ' or ".

At this point, the only solution I can think of is to go through the incoming string character by character and change all occurrences of ''' to its html code equivalent.

Is there a cleaner more work intensive (for me :) ) solution?

Thanks.
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

why don't u try with addslashes() and stripslashes() without the 'c'?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

stripcslashes recognizes \n, \r, octal and hex representation, whereas stripslashes doesn't. The carriage return recognition is the primary reason why I don't use stripslashes. I have tested using stripslashes() as well, just to try, and it didn't change anything. Thanks though.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

FYI: Here's the function I wrote - as far as I can tell it should do the same thing as addslashes, but alas addslashes does something funky, and not what I need.

Code: Select all

function custom_add_slashes($followup_info)
{
  $size = strlen($followup_info);
  for($x = 0;$x<$size;$x++)
  {
   $letters[] = substr($followup_info,$x,1);
  }
  $fixed_array;
  for($index = 0;$current_char = $letters[$index];$index++)
  {
   if($current_char == """ || $current_char == "'" || $current_char == "")
   {
    $fixed_array[] = "";
   }
   fixed_array[] = $current_char;
 }
 return(implode('',$fixed_array));
}
Post Reply