CFLib.org – Common Function Library Project

convertPhoneLetters([oldNumber])

Last updated August 26, 2012

author

Brian Swartzfager

Version: 1 | Requires: CF9 | Library: StrLib

Description:
Converts the letters in a phone number to their numeric keypad equivalent (eg: a=>2, k=>5, p=>7, etc). Coded for phones using ISO/IEC 9995-8 (http://en.wikipedia.org/wiki/ISO/IEC_9995#ISO.2FIEC_9995-8), but easily changed.

Return Values:
A string with letters converted back to digits

Example:

<cfscript>
testNums= ["1-800-TRY-FOGO","555-212-cfml"];

for(x=1; x <= ArrayLen(testNums); x++) {
  result= convertPhoneLetters(testNums[x]);
  writeOutput(result & "<br />");
}
</cfscript>

Parameters:

Name Description Required
oldNumber A string representing the phone number to convert No

Full UDF Source:

/**
 * Converts the letters in a phone number to the corresponding digits on an American phone.
 * version 1.0 by Brian Swartzfager
 * 
 * @param oldNumber      A string representing the phone number to convert (Optional)
 * @return A string with letters converted back to digits 
 * @author Brian Swartzfager (bcswartz@gmail.com) 
 * @version 1.0, August 26, 2012 
 */
public string function convertPhoneLetters(required string oldNumber) {
  var newNumber= arguments.oldNumber;
  var regArray= [
    "[A-Ca-c]",
    "[D-Fd-f]",
    "[G-Ig-i]",
    "[J-Lj-l]",
    "[M-Om-o]",
    "[P-Sp-s]",
    "[T-Vt-v]",
    "[W-Zw-z]"
  ];
        
  var resultArray= [
    2,3,4,5,6,7,8,9
  ];
        
  for (var x=1; x <= arrayLen(regArray); x++) {
    newNumber= REReplace(newNumber,regArray[x],resultArray[x],"all");
  }
        
  return newNumber;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson