Jump to content


Photo

addslashes to an array? is it possible *SOLVED*


  • Please log in to reply
5 replies to this topic

#1 JimChuD

JimChuD
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 04 July 2006 - 08:42 AM

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

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 04 July 2006 - 09:14 AM

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;
}


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 JimChuD

JimChuD
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 04 July 2006 - 09:32 AM

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

#4 JimChuD

JimChuD
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 04 July 2006 - 09:36 AM

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

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 04 July 2006 - 09:58 AM

no problam enjoy
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 04 July 2006 - 01:27 PM

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;
}


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);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users