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

inserting a character at a certain spot in a string

Post by Luke »

I need to take a phone number and insert a - as the fourth character, making 5551234 into 555-1234. How would I go about doing so?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

store the number in an array. what I did at least. In hindsight,format your numbers beforehand;)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Store the number in an array? I don't understand your response.

I never like to format anything when it is being stored in a database, because I like to be able to tear it apart any way I like after it comes out.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

use substr to grab the first 3 characters, then again to grab the last 4 digits. combine them with a dash using conconation operator
$firstthree.'-'.$lastfour
I never like to format anything when it is being stored in a database, because I like to be able to tear it apart any way I like after it comes out.
I agree, it saves time when you want to switch to a new format, and not only that you're saving one character in each field, in large tables formatting characters are a waste of space
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

how many different ways can ya format a phone number? :?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

530-555-1234
(530) 555-1234
530 555-1234
5305551234
555-1234
5551234
530.555.1234
530 555.1234
555.1234
(530) 555.1234

Uhh... 10 that I can think of.
Last edited by Luke on Thu Nov 03, 2005 5:34 pm, edited 3 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

psst.. (I believe that question was rhetorical)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I dont.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So do you need to reformat all those myriad possibilities and make them just 123-4567? This is untested, but it should work:

Code: Select all

$number = '555.1234';
$digit_count = 0;
//clean out the non-digit characters.  There's probably an easier/more efficient way
for($i = 0;$i < strlen($number);++$i)
{
   //if the current character is a digit
   if(preg_match('/\d/',$number{$i}))
   {
      //increment the count of digits
      ++$digit_count;
      //if we're looking at the fourth digit, put in a dash
      //before we add the digit
      if($digit_count == 4)
     {
         $formatted_number[] = '-';
     }
     $formatted_number[] = $number{$i};
   }
}
//$formatted_number should now contain '555-1234';
You only asked to put the 4th character in as a dash - so this doesn't handle numbers with area codes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

thanks pickle. :-D

I don't need to format it in all those ways... I was just illustrating how many ways you can format a phone number....

Needs to go from 1234567 to 123-4567.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

How about...

Code: Select all

<?php
$number = '555.1234';
$number = substr($number = preg_replace('/[^0-9]/', '', $number), 0, 3) . '-' . substr($number, 3);
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

redmonkey wrote:How about...

Code: Select all

<?php
$number = '555.1234';
$number = substr($number = preg_replace('/[^0-9]/', '', $number), 0, 3) . '-' . substr($number, 3);
?>
Genius.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Ah, missed that reply as I was posting, if there is no requirement to strip anything first...

Code: Select all

<?php
$number = '5551234';
$number = substr($number, 0, 3) . '-' . substr($number, 3);
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

My advise is to store them without any formatting.. It's a phonenumber.. :)

When displaying them to the user you can offcourse use the prefered/localized format..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

timvw wrote:My advise is to store them without any formatting.. It's a phonenumber.. :)

When displaying them to the user you can offcourse use the prefered/localized format..
I tend not to format anything when storing in a db. Let's see is there anything I format? uhh...

not even name capitolisation... I use ucwords().

I can't think of anything I format before entering in database.
Post Reply