Jump to content

Archived

This topic is now archived and is closed to further replies.

JimChuD

addslashes to an array? is it possible *SOLVED*

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

Share this post


Link to post
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;
}

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
actually tell a lie that seems to now be working with it.
I think i put a typo in.

I appreciate the assistance :)

Kind Regards
Jim

Share this post


Link to post
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);

Share this post


Link to post
Share on other sites

×

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.