PHP GSM Character Set Validation
September 17th, 2009I recently encountered a somewhat tough probelm in validating a string as having valid GSM characters such that when sent in a text message, there would be no problems displaying the text. If you need more info on GSM characters, there is a great resource here. The code is pretty simple. It starts with a large PHP array of all the ascii codes for the GSM characters, then does a simple walk through the string checking if each character’s ascii value is in the Array.
$valid_gsm_keycodes = Array(
0×0040, 0×0394, 0×0020, 0×0030, 0×00a1, 0×0050, 0×00bf, 0×0070,
0×00a3, 0×005f, 0×0021, 0×0031, 0×0041, 0×0051, 0×0061, 0×0071,
0×0024, 0×03a6, 0×0022, 0×0032, 0×0042, 0×0052, 0×0062, 0×0072,
0×00a5, 0×0393, 0×0023, 0×0033, 0×0043, 0×0053, 0×0063, 0×0073,
0×00e8, 0×039b, 0×00a4, 0×0034, 0×0035, 0×0044, 0×0054, 0×0064, 0×0074,
0×00e9, 0×03a9, 0×0025, 0×0045, 0×0045, 0×0055, 0×0065, 0×0075,
0×00f9, 0×03a0, 0×0026, 0×0036, 0×0046, 0×0056, 0×0066, 0×0076,
0×00ec, 0×03a8, 0×0027, 0×0037, 0×0047, 0×0057, 0×0067, 0×0077,
0×00f2, 0×03a3, 0×0028, 0×0038, 0×0048, 0×0058, 0×0068, 0×0078,
0×00c7, 0×0398, 0×0029, 0×0039, 0×0049, 0×0059, 0×0069, 0×0079,
0×000a, 0×039e, 0×002a, 0×003a, 0×004a, 0×005a, 0×006a, 0×007a,
0×00d8, 0×001b, 0×002b, 0×003b, 0×004b, 0×00c4, 0×006b, 0×00e4,
0×00f8, 0×00c6, 0×002c, 0×003c, 0×004c, 0×00d6, 0×006c, 0×00f6,
0×000d, 0×00e6, 0×002d, 0×003d, 0×004d, 0×00d1, 0×006d, 0×00f1,
0×00c5, 0×00df, 0×002e, 0×003e, 0×004e, 0×00dc, 0×006e, 0×00fc,
0×00e5, 0×00c9, 0×002f, 0×003f, 0×004f, 0×00a7, 0×006f, 0×00e0 );
for($i = 0; $i < strlen($string); $i++) {
if(!in_array($string[$i], $valid_gsm_keycodes)) return false;
}
I hope this code is helpful and will save someone else some time, since I didn’t find any other resources online.