Page 2 of 2

Posted: Thu Nov 03, 2005 6:32 pm
by Luke

Code: Select all

function formatPhone($number, $delim="-"){
	$number = preg_replace('/[^0-9]/', '', $number);
	$length = strlen($number);
	if($length == 7){
		$number = substr($number, 0, 3) . $delim . substr($number, 3);
	}
	elseif($length == 10){
		$number = substr($number, 0, 3) . $delim . substr($number, 3, 3) . $delim . substr($number, 6);
	}
	else{
		return false;
	}
    return $number;
}
Well here's your finished product. Thanks fellas!

Posted: Thu Nov 03, 2005 6:37 pm
by Charles256
the question was rhetorical......but go give yourself a cookie since you thought of ten ways. congratulations.

Posted: Thu Nov 03, 2005 6:41 pm
by Luke
Charles256 wrote:the question was rhetorical......but go give yourself a cookie since you thought of ten ways. congratulations.
Lol... K
::eats rhetorical cookie::

Posted: Thu Nov 03, 2005 6:43 pm
by redmonkey
As I'm in a giving mood tonight :lol: I'd of gone for something along the lines of this.....

Code: Select all

<?php
function formatPhone($num, $delim = '-')
{
  $num = preg_replace('/[^0-9]/', '', $num);
  
  switch(strlen($num))
  {
    case  7:
      return substr($num, 0, 3) . $delim . substr($num, 3);
      
    case 10:
      return substr($num, 0, 3) . $delim . substr($num, 3, 3) . $delim . substr($num, 6);
      
    default:
      return false;
  }
}
?>

Posted: Thu Nov 03, 2005 6:55 pm
by Luke
Nice... shorter. Thanks.

Posted: Thu Nov 03, 2005 7:08 pm
by timvw
I didn't read your question well.. So i posted bogus... As an excuse, something even shorter ;)

Code: Select all

function formatPhone($num, $delim = '-')
{
        $num = preg_replace('#[^0-9]#', '', $num);
        $num = preg_replace('#^(\d{3})(\d{4})$#', "$1{$delim}$2", $num);
        $num = preg_replace('#^(\d{3})(\d{3})(\d{4})$#', "$1{$delim}$2{$delim}$3", $num);
        return $num;
}
Edit: Although it's shorter, it's slower... I feel ashamed :P time to go to bed :) Night All.

Posted: Thu Nov 03, 2005 7:44 pm
by John Cartwright
timvw wrote:Edit: Although it's shorter, it's slower... I feel ashamed Razz time to go to bed Smile Night All.
But not by that much, most likely you will not even notice a difference. Although, it is best to get in the habbit to avoid initializing regex engine when possible.

Posted: Thu Nov 03, 2005 7:53 pm
by redmonkey
Jcart wrote:
timvw wrote:Edit: Although it's shorter, it's slower... I feel ashamed Razz time to go to bed Smile Night All.
But not by that much, most likely you will not even notice a difference. Although, it is best to get in the habbit to avoid initializing regex engine when possible.
I haven't checked performance and I'm not likely to either, I'll take your word on it. However, the pure preg function posted also doesn't return false for numbers that are not 7 or 10 digits in length, it's just blindly working away which is not (in my opinion) the best approach.

I could still shave a little of that function if I were being really picky :lol:

Posted: Thu Nov 03, 2005 8:18 pm
by feyd
regex is pretty much guaranteed slower than substr() ...

Posted: Thu Nov 03, 2005 8:23 pm
by John Cartwright
redmonkey wrote:I haven't checked performance and I'm not likely to either, I'll take your word on it. However, the pure preg function posted also doesn't return false for numbers that are not 7 or 10 digits in length, it's just blindly working away which is not (in my opinion) the best approach.

I could still shave a little of that function if I were being really picky :lol:
I did a quick benchmark of the two functions. My format was a loop of 100, performed 50 times (so it was performed 5000 times).

Code: Select all

function formatPhone($num, $delim = '-')
{
  $num = preg_replace('/[^0-9]/', '', $num);
  
  switch(strlen($num))
  {
    case  7:
      return substr($num, 0, 3) . $delim . substr($num, 3);
      
    case 10:
      return substr($num, 0, 3) . $delim . substr($num, 3, 3) . $delim . substr($num, 6);
      
    default:
      return false;
  }
}
Total Average: 0.01634706 seconds through 50 times.

Code: Select all

function formatPhone($num, $delim = '-')
	{
			  $num = preg_replace('#[^0-9]#', '', $num);
			  $num = preg_replace('#^(\d{3})(\d{4})$#', "$1{$delim}$2", $num);
			  $num = preg_replace('#^(\d{3})(\d{3})(\d{4})$#', "$1{$delim}$2{$delim}$3", $num);
			  return $num;
	}
Total Average: 0.02379152 seconds through 50 times.