Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#31683 closed feature request (wontfix)

Adding standard functions for parsing phone numbers

Reported by: hinjiriyo's profile Hinjiriyo Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1.1
Component: Formatting Keywords:
Focuses: Cc:

Description

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>

Change History (4)

#1 @shooper
10 years ago

Two thoughts:

  1. Great idea, however not sure if it belongs in core. Sanitizing URLs and e-mail addresses makes sense as both of those types of data are stored as part of the default core data (email in _users and URLs in the user_url usermeta). Telephone numbers would be a custom field.
  1. If this were to be added to core, my second thought would be how does it handle telephone extensions, like:

+1-613-555-1212 x 200

#2 @Hinjiriyo
10 years ago

Your number would be escaped to

001613555121220 // omitting last zero because too long

#3 @Hinjiriyo
10 years ago

My simple logic on why this belongs to the core is:

The protocols for URLs ('http:') and mailaddresses ('mailto:') are standard protocols.

Wordpress core offers functions for URLs and mailaddresses.

Protocols for phone numbers ('tel:') and fax numbers ('fax:') are standard protocols.

Wordpress core can offer function for them. Because they are standards.

#4 @helen
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

@shooper's comment is precisely correct. We have functions for field types we store and use out of the box, and phone number formatting varies quite widely making a good data validation approach specific to each implementation. Including a function that does some things but not others is unpleasant territory, particularly once sanitization and conversion/transformation are mixed together as per your proposal.

Note: See TracTickets for help on using tickets.