Skip to content

Instantly share code, notes, and snippets.

@maddisondesigns
Last active January 4, 2023 17:20
Show Gist options
  • Save maddisondesigns/a856692f67bff38e8ee40dc5bf2ee41f to your computer and use it in GitHub Desktop.
Save maddisondesigns/a856692f67bff38e8ee40dc5bf2ee41f to your computer and use it in GitHub Desktop.
Convert Phone Word to Phone Number
<?php
/**
* Convert a phone number containing words, to a plain number. e.g. '1800 CALLME' == '1800225563'
*/
function ephemeris_phone_word_to_number( $phone_number ) {
$phone_word_pattern = array(
'a' => '2',
'b' => '2',
'c' => '2',
'd' => '3',
'e' => '3',
'f' => '3',
'g' => '4',
'h' => '4',
'i' => '4',
'j' => '5',
'k' => '5',
'l' => '5',
'm' => '6',
'n' => '6',
'o' => '6',
'p' => '7',
'q' => '7',
'r' => '7',
's' => '7',
't' => '8',
'u' => '8',
'v' => '8',
'w' => '9',
'x' => '9',
'y' => '9',
'z' => '9',
' ' => '',
'-' => '',
'(' => '',
')' => '',
'[' => '',
']' => '',
);
if( !empty( $phone_number ) ) {
return str_ireplace( array_keys( $phone_word_pattern ), array_values( $phone_word_pattern ), $phone_number );
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment