Stefan83 Posted June 24, 2014 Share Posted June 24, 2014 Hi - I'm using Wordpress gravity forms plugin. I'd like to force capitalise and remove the spaces within a specific field. This is what I have but its not working. $_POST[$each] = strtoupper(rgpost($each)) works for making the characters capitals but adding && str_replace(' ', '', $each) after doens't work. What am I doing wrong? Thanks add_action('gform_pre_submission_22', 'capitalize_fields'); function capitalize_fields($form){ $fields_to_cap = array( 'input_64' ); foreach ($fields_to_cap as $each) { $_POST[$each] = strtoupper(rgpost($each)) && str_replace(' ', '', $each); } return $form; } ?> Link to comment https://forums.phpfreaks.com/topic/289272-capitalize-and-remove-spaces-after-form-submission/ Share on other sites More sharing options...
maxxd Posted June 24, 2014 Share Posted June 24, 2014 '&&' is a logical operator - it doesn't work the way you're trying to use it. Check http://us3.php.net/manual/en/language.operators.logical.php for more details. In the meantime, try foreach($fields_to_cap as $field){ $cap_fields[] = strtoupper(str_replace(' ','',rgpost($each))); } I'm assuming rgpost() is a custom function that should be called on the raw data? Link to comment https://forums.phpfreaks.com/topic/289272-capitalize-and-remove-spaces-after-form-submission/#findComment-1483131 Share on other sites More sharing options...
mogosselin Posted June 24, 2014 Share Posted June 24, 2014 For the uppercase, there is also array_change_key_case. Of course, if you need to also remove the spaces, you'll have to use another function. There is also array_map So you could code something like this: function cap_field($field_to_cap) { $rgpost_field = rgpost($field_to_cap); // no idea what rgpost does $space_removed = trim($rgpost_field); // use trim() if you want to remove only the spaces at the beginning and end of the field. $uppercase = strtoupper($space_removed); return $uppercase; } $cap_fields = array_map("cap_field", $fields_to_cap); Instead of the 4 lines there, you could use the line maxxd wrote to you: strtoupper(str_replace(' ','',rgpost($each))); But, it'll be easier to debug for you. This way, you can var_dump one line after the other. If it works, change it with maxxd's line. Link to comment https://forums.phpfreaks.com/topic/289272-capitalize-and-remove-spaces-after-form-submission/#findComment-1483137 Share on other sites More sharing options...
Psycho Posted June 24, 2014 Share Posted June 24, 2014 I was going to post almost the exact same thing as mogosselin, but he beat me to it. Using array_map() with a separate function allows you to decouple the manipulation of the input from the processing of the input. Plus, this is a personal preference, I would put each step of the string manipulation on a separate line instead of one line. Makes it easier to "see" what is going on when you have to debug issues later. Then again, since rpost is a custom function, you could potentially update that to do the additional work and call it using array_map(). FYI: The fact that no one has a clue what rgpost() does is a good indication that it is not well named. Don't be afraid of a long name if it helps to identify it's purpose and/or what it does. Link to comment https://forums.phpfreaks.com/topic/289272-capitalize-and-remove-spaces-after-form-submission/#findComment-1483138 Share on other sites More sharing options...
maxxd Posted June 24, 2014 Share Posted June 24, 2014 I didn't even think about array_map() - much better for readability. Link to comment https://forums.phpfreaks.com/topic/289272-capitalize-and-remove-spaces-after-form-submission/#findComment-1483144 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.