inserting a character at a certain spot in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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!
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

the question was rhetorical......but go give yourself a cookie since you thought of ten ways. congratulations.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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::
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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;
  }
}
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Nice... shorter. Thanks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

regex is pretty much guaranteed slower than substr() ...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply