validate form fields - remove/trim blank spaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

validate form fields - remove/trim blank spaces

Post by cjkeane »

hi everyone.

i'm using the following function to apply mysql_real_escape string and htmlentities to form fields. is there a way to modify it to trim white space (left and right) from form fields? i'm not quite sure how to go about doing it.

Code: Select all

<?php
function safe($value){ 
   return mysql_real_escape_string(htmlentities($value)); 
} 
?>
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: validate form fields - remove/trim blank spaces

Post by greyhoundcode »

You could use the trim() function.

Code: Select all

return mysql_real_escape_string(htmlentities(trim($value))); 
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: validate form fields - remove/trim blank spaces

Post by cjkeane »

thanks. i was looking at trim, but i wasn't sure if it would need to cycle through all form fields or just add the trim function to my existing function. i'll give it a try.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: validate form fields - remove/trim blank spaces

Post by greyhoundcode »

cjkeane wrote:but i wasn't sure if it would need to cycle through all form fields or just add the trim function to my existing function.
Depending on your intentions you could of course place either your modified function or just trim by itself in a for/foreach loop, or something like that.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: validate form fields - remove/trim blank spaces

Post by cjkeane »

i had to use a for each function with trim in order to check each field before data was posted.

thanks for the help. it got me going in the right direction.

problem solved!
Post Reply