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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson