[wp-trac] [WordPress Trac] #31683: Adding standard functions for parsing phone numbers

WordPress Trac noreply at wordpress.org
Wed Mar 18 13:41:42 UTC 2015


#31683: Adding standard functions for parsing phone numbers
-----------------------------+-----------------------------
 Reporter:  Hinjiriyo        |      Owner:
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Formatting       |    Version:  4.1.1
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 There are functions in the WP core for testing and sanitizing URLs and
 mail addresses.

 Since the protocols 'tel' and 'fax' are standard protocols, see RFC 2806
 and RFC 3966, standard functions for testing and sanitizing phone numbers
 (and fax numbers, mobile numbers) are helpful.

 '''Draft of a test function:'''
 {{{
 // check if the number is callable by technical devices
 function is_phonenumber ( $string ) {
         if ( is_string( $string ) ) {
                 return (bool) preg_match( '/^[+]?[0-9 ]{2,14}[0-9]$/',
 $string );
         } else {
                 return false;
         }
 }
 }}}


 '''Draft of a sanitizing function:'''

 {{{
 // makes a visual reprentation of a phone number callable by technical
 devices
 function esc_phonenumber ( $tel ) {

         // only strings
         if ( ! is_string( $tel ) ) {
                 return '';
         }

         // remove invalid chars
         $tel = preg_replace( '/[^0-9a-z+]/i', '', $tel );

         // remove plus sign within the number, only keep it at the start
         $tel_first_sign = substr( $tel, 0, 1 );
         $tel_substr = substr( $tel, 1 );
         $tel_substr = preg_replace( '/[+]/', '', $tel_substr );
         $tel = $tel_first_sign . $tel_substr;

         // convert vanity numbers
         if ( preg_match( '/[a-z]/i', $tel ) ) {
                 $tel = preg_replace( '/[abc]/i', '2', $tel );
                 $tel = preg_replace( '/[def]/i', '3', $tel );
                 $tel = preg_replace( '/[ghi]/i', '4', $tel );
                 $tel = preg_replace( '/[jkl]/i', '5', $tel );
                 $tel = preg_replace( '/[mno]/i', '6', $tel );
                 $tel = preg_replace( '/[pqrs]/i', '7', $tel );
                 $tel = preg_replace( '/[tuv]/i', '8', $tel );
                 $tel = preg_replace( '/[wxyz]/i', '9', $tel );
         }

         // E.164: maximum number of digits: 15
         $tel = substr( $tel, 0, 15 );

         // change country area sign
         $tel = preg_replace( '|^[+]|i', '00', $tel );

         // return sanitized phone number
         return $tel;

 }

 }}}

 In use:

 {{{
 <?php
 $tel = '+49 (0)89 / 123 45-120';
 // or: $tel = '+1-44-HOLIDAY';
 $escaped_tel = esc_phonenumber ( $tel );
 if ( is_phonenumber( $escaped_tel ) ) {
 ?>
 <a href="tel:<?php echo $escaped_tel; ?>">Call John at <?php echo $tel;
 ?></a>
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31683>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list