Advertisement
chrishajer

convert value to hex for storage and back for display

Nov 3rd, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2.  
  3. // http://www.gravityhelp.com/forums/topic/input-on-single-line-text-is-cut-off-after
  4. // this handles converting the entered value to hex for storage
  5. //
  6. // strtohex and hextostr functions lifted from here:
  7. // http://www.php.net/manual/en/function.hexdec.php#100578
  8. //
  9. // change the 60 here to your form ID
  10. add_filter('gform_pre_submission_filter_60', 'ch_strtohex');
  11. function ch_strtohex($form) {
  12.     // input 4 is the form field I want to convert to hex
  13.     $x = rgpost('input_4');
  14.     $s='';
  15.     // convert the submitted string to hex
  16.     foreach(str_split($x) as $c)
  17.         $s .= sprintf('%02X',ord($c));
  18.     // assign the hex value to the POST field
  19.     $_POST['input_4'] = $s;
  20.     // return the form
  21.     return $form;
  22. }
  23.  
  24. // http://www.gravityhelp.com/forums/topic/input-on-single-line-text-is-cut-off-after
  25. // retrieve hex and convert before displaying in the admin: single entry view
  26. add_filter('gform_entry_field_value', 'ch_hextostr_single', 10, 4);
  27. function ch_hextostr_single($x, $field, $lead, $form) {
  28.     // run this code on form 60, field 4 only
  29.     // change to match your form values
  30.     if ($form['id'] == 60 && $field['id'] == 4) {
  31.         $s='';
  32.         foreach(explode("\n",trim(chunk_split($x,2))) as $h) {
  33.             $s .= chr(hexdec($h));
  34.         }
  35.         // prevent rendering anything that looks like HTML as HTML
  36.         return htmlspecialchars($s);
  37.     }
  38.     else {
  39.         // not (form 60 and field 4), return the original value
  40.         return $x;
  41.     }
  42. }
  43.  
  44. // http://www.gravityhelp.com/forums/topic/input-on-single-line-text-is-cut-off-after
  45. // retrieve hex and convert before displaying in the admin: entry list view
  46. // note the different filter name here "entries"
  47. add_filter('gform_entries_field_value', 'ch_hextostr_list', 10, 3);
  48. function ch_hextostr_list($x, $form_id, $field_id) {
  49.     // run this code on form 60, field 4 only
  50.     // change to match your form values
  51.     if ($form_id == 60 && $field_id == 4) {
  52.         $s='';
  53.         foreach(explode("\n",trim(chunk_split($x,2))) as $h) {
  54.             $s .= chr(hexdec($h));
  55.         }
  56.         // prevent rendering anything that looks like HTML as HTML
  57.         return htmlspecialchars($s);
  58.     }
  59.     else {
  60.         // not (form 60 and field 4), return the original value
  61.         return $x;
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement