Page 1 of 2

inserting a character at a certain spot in a string

Posted: Thu Nov 03, 2005 3:21 pm
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?

Posted: Thu Nov 03, 2005 3:33 pm
by Charles256
store the number in an array. what I did at least. In hindsight,format your numbers beforehand;)

Posted: Thu Nov 03, 2005 3:48 pm
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.

Posted: Thu Nov 03, 2005 3:51 pm
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

Posted: Thu Nov 03, 2005 4:04 pm
by Charles256
how many different ways can ya format a phone number? :?

Posted: Thu Nov 03, 2005 5:31 pm
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.

Posted: Thu Nov 03, 2005 5:32 pm
by John Cartwright
psst.. (I believe that question was rhetorical)

Posted: Thu Nov 03, 2005 5:32 pm
by Luke
I dont.

Posted: Thu Nov 03, 2005 6:00 pm
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.

Posted: Thu Nov 03, 2005 6:08 pm
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.

Posted: Thu Nov 03, 2005 6:09 pm
by redmonkey
How about...

Code: Select all

<?php
$number = '555.1234';
$number = substr($number = preg_replace('/[^0-9]/', '', $number), 0, 3) . '-' . substr($number, 3);
?>

Posted: Thu Nov 03, 2005 6:14 pm
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.

Posted: Thu Nov 03, 2005 6:15 pm
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);
?>

Posted: Thu Nov 03, 2005 6:18 pm
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..

Posted: Thu Nov 03, 2005 6:21 pm
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.