Solved

PHP trim and addslashes to multiple input variables

Posted on 2007-04-09
6
1,904 Views
Last Modified: 2013-12-12
Is there a simple way of trimming and adding slashes to multiple, incoming $_POST or $_GET variables in PHP?

I have several input variables coming in (i.e. $_POST['first'],$_POST['last']) and I'd like to loop through them all, trim any newlines or spaces on either end, and add slashes for MySQL all at once, instead of:

$first = addslashes($_POST['first']);
$last = addslashes($_POST['last']);
$first = trim($first);
$last = trim($last);

I don't want to use magic_quotes because I need more control over where and when the form variable characters are escaped.

Thanks
$marc

0
Comment
Question by:marcparillo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 18877518
<?php
$arrMyPostVariables = array()

foreach ($_POST as $field=>$value)
{
   $arrMyPostVariables[$field] = addslashes($value);
}
?>

Then, you can cycle through or retrieve values from $arrMyPostVariables as needed.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18877522
I apologize, I missed a semicolon:

<?php
$arrMyPostVariables = array();
...
0
 
LVL 3

Author Comment

by:marcparillo
ID: 18877566
Thanks,

Could I also add a trim() command to your suggestion?

foreach ($_POST as $field=>$value)
{
   $arrMyPostVariables[$field] = addslashes($value);
   $arrMyPostVariables[$field] = trim($value);
}

And then, to extract the values from the new array, I just call them as needed? Like this? --

$arrMyPostVariables['first']
$arrMyPostVariables['last']


0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 24

Accepted Solution

by:
glcummins earned 200 total points
ID: 18877622
Yes, you can certainly add trim, or any other function. In fact, you can condense the code like this:

foreach ($_POST as $field=>$value)
{
   $arrMyPostVariables[$field] = trim(addslashes($value));
}

To extract the values, you will use your field names from your form. So, if you have a field named 'username', you would access it's value like this:

$arrMyPostVariables['username'];
0
 
LVL 3

Author Comment

by:marcparillo
ID: 18877636
Excellent!
Exactly what I needed. Thank You!
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 18877639
Take a look at mysql_real_escape_string() also.  

http://www.php.net/manual/en/function.mysql-real-escape-string.php
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question