-
-
Save spivurno/37a19bc9eec1bf13cfbb1f2ab8a71c94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'gform_merge_tag_filter', 'convert_number_to_words_modifier', 10, 4 ); | |
function convert_number_to_words_modifier( $value, $merge_tag, $modifier, $field ) { | |
// Check if the modifier is "words" | |
if ( $modifier == 'words' ) { | |
// Convert the numeric value to words | |
$value = convert_number_to_words( $value ); | |
} | |
// Return the modified value | |
return $value; | |
} | |
function convert_number_to_words( $number ) { | |
// Array of numbers as words | |
$numbers_as_words = array( | |
0 => 'zero', | |
1 => 'one', | |
2 => 'two', | |
3 => 'three', | |
4 => 'four', | |
5 => 'five', | |
6 => 'six', | |
7 => 'seven', | |
8 => 'eight', | |
9 => 'nine', | |
); | |
// Convert the number to a string | |
$number_string = (string) $number; | |
// Initialize the output string | |
$output = ''; | |
// Split the number into its individual digits | |
$digits = str_split( $number_string ); | |
// Loop through the digits | |
foreach ( $digits as $digit ) { | |
// Add the word for this digit to the output string | |
$output .= $numbers_as_words[ $digit ] . ' - '; | |
} | |
// Remove the last hyphen from the output string | |
$output = rtrim( $output, ' - ' ); | |
// Return the output string | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment