JimChuD Posted July 4, 2006 Share Posted July 4, 2006 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.regardsJim Quote Link to comment https://forums.phpfreaks.com/topic/13624-addslashes-to-an-array-is-it-possible-solved/ Share on other sites More sharing options...
redarrow Posted July 4, 2006 Share Posted July 4, 2006 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 Link to comment https://forums.phpfreaks.com/topic/13624-addslashes-to-an-array-is-it-possible-solved/#findComment-52804 Share on other sites More sharing options...
JimChuD Posted July 4, 2006 Author Share Posted July 4, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13624-addslashes-to-an-array-is-it-possible-solved/#findComment-52805 Share on other sites More sharing options...
JimChuD Posted July 4, 2006 Author Share Posted July 4, 2006 actually tell a lie that seems to now be working with it.I think i put a typo in.I appreciate the assistance :)Kind RegardsJim Quote Link to comment https://forums.phpfreaks.com/topic/13624-addslashes-to-an-array-is-it-possible-solved/#findComment-52806 Share on other sites More sharing options...
redarrow Posted July 4, 2006 Share Posted July 4, 2006 no problam enjoy Quote Link to comment https://forums.phpfreaks.com/topic/13624-addslashes-to-an-array-is-it-possible-solved/#findComment-52812 Share on other sites More sharing options...
toplay Posted July 4, 2006 Share Posted July 4, 2006 [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); Quote Link to comment https://forums.phpfreaks.com/topic/13624-addslashes-to-an-array-is-it-possible-solved/#findComment-52870 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.