Jump to content

addslashes to an array? is it possible *SOLVED*


JimChuD

Recommended Posts

Hi,
I am currently having a problem with a set of arrays i use.
Since i have turned off magic quotes i am no longer getting \'s in the database. Due to this when i pull the information out and try and reuse it in other queries it obviously fails due to any slashes i have in the data.

I pull out this information in to arrays and then use it with $var[column name] however i dont want to have to add slashes indevidually to each variable as at times there is alot of results in an array.

I have looked about however to no avail and i cant find a array command to pull up the headers (column names) for the arrays to write a usable function for it?

any help would be appreciated.
regards
Jim
Link to comment
Share on other sites

Say you have a $_POST array and and you don't know if magic quotes is on:
Then simply call this routine and your entire array is reslashed if magic quotes are off or its left alone if they are turned on.

array_walk($_POST, 'reslash_multi');

function reslash_multi(&$val,$key)
{
  if (is_array($val)) array_walk($val,'reslash_multi',$new);
  else {
      $val = reslash($val);
  }
}


function reslash($string)
{
  if (!get_magic_quotes_gpc())$string = addslashes($string);
  return $string;
}

Link to comment
Share on other sites

hey thanks for the prompt response.. :)

i tried calling that with my currently pulled up array (nonpost) and it doesnt seem to make any difference.

i see that it should open each value when it goes through the array and then should call the reslash function to then addslashes to the variable.
i just cant see why its not working
Link to comment
Share on other sites

[quote author=redarrow link=topic=99386.msg391354#msg391354 date=1152004466]
Say you have a $_POST array and and you don't know if magic quotes is on:
Then simply call this routine and your entire array is reslashed if magic quotes are off or its left alone if they are turned on.

array_walk($_POST, 'reslash_multi');

function reslash_multi(&$val,$key)
{
  if (is_array($val)) array_walk($val,'reslash_multi',$new);
  else {
      $val = reslash($val);
  }
}


function reslash($string)
{
  if (!get_magic_quotes_gpc())$string = addslashes($string);
  return $string;
}
[/quote]

FYI - Tips:

You have an undefined $new variable in the code example.

To save having to call get_magic_quotes_gpc() function all the time, I recommend setting a constant at the beginning of a script and refer to that. Example:

define('MAGIC_QUOTES', (boolean) get_magic_quotes_gpc());

function reslash($string) {
  return MAGIC_QUOTES ? $string : addslashes($string);
}

If you have PHP 5, then you can use the recursive function and shorten "reslash_multi". Example:

array_walk_recursive($_POST, 'reslash_multi');

function reslash_multi(&$val, $key) {
    $val = reslash($val);
}

On the other hand, it's a paste of CPU/time to have to iterate through the array when magic quotes is already on. So, it's best to check that first. Also, if you know that your array doesn't contain nested arrays, then you can shorten it to this:

if (!MAGIC_QUOTES)
    $ary = array_map('addslashes', $ary);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.