/ Published in: PHP
This snippet is usable for telephone number normalization. It supports re-mapping of misused alphabetical characters (e.g. i for 1) and some other clean up stuff... Take a look.
Update, 2009-10-02: added function to create a corresponding MySQL REPLACE construct to normalize data which is already stored in database. I decided to use this approach because MySQL's regular expression engine is very slow. TODO: add the possibility to remove non digit characters at the end of the laaaarge REPLACE call.
Update, 2009-10-02: added function to create a corresponding MySQL REPLACE construct to normalize data which is already stored in database. I decided to use this approach because MySQL's regular expression engine is very slow. TODO: add the possibility to remove non digit characters at the end of the laaaarge REPLACE call.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // country prefix normalization '+00' => '+', '++' => '+', // country prefix is always 00 '+' => '00', // funny user input goulash 'i' => '1', 'I' => '1', 'l' => '1', 'o' => '0', 'O' => '0', // ([^\diIloO\+]*) // ...brackets '(' => '', ')' => '', '[' => '', ']' => '', '[' => '', ']' => '', // slashes '/' => '', '\\\\' => '', // dashes '-' => '', '_' => '', // whitespaces ' ' => '' ); function normalizeTelephoneNumber($telephone_number, $search_replace_mapping) { // fetch search and replace arrays // simple string replacement // lets kick out all dutty stuff which is left... return $telephone_number; } function generateSqlReplaceStatement($telephone_number, $search_replace_mapping) { $s = ''; $template = 'REPLACE(%s, \'%s\', \'%s\')'; $i = 0; foreach ($search_replace_mapping as $search => $replace) { $i++; } $s = 'SELECT '. $s .' AS normalized FROM Accommodation'; return $s; } '0049 03831 667 156', '+39 0471 / 975 642', '+0039 6757 - 3939 9393', '+49 (0) 3834 50 77 73', '+43 (i) 4m 4n idiOt', '+44 (0) 1234 \\ 55 55' ); foreach ($numbers as $number) { $numbers_clean[] = normalizeTelephoneNumber($number, $search_replace_mapping); } foreach ($numbers as $number) { $sql[$number] = generateSqlReplaceStatement($number, $search_replace_mapping); } ?>