inserting a character at a certain spot in a string
Moderator: General Moderators
inserting a character at a certain spot in a string
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
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
$firstthree.'-'.$lastfour
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 spaceI 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.
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
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.
(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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
So do you need to reformat all those myriad possibilities and make them just 123-4567? This is untested, but it should work:
You only asked to put the 4th character in as a dash - so this doesn't handle numbers with area codes.
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';Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
How about...
Code: Select all
<?php
$number = '555.1234';
$number = substr($number = preg_replace('/[^0-9]/', '', $number), 0, 3) . '-' . substr($number, 3);
?>Genius.redmonkey wrote:How about...Code: Select all
<?php $number = '555.1234'; $number = substr($number = preg_replace('/[^0-9]/', '', $number), 0, 3) . '-' . substr($number, 3); ?>
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);
?>I tend not to format anything when storing in a db. Let's see is there anything I format? uhh...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..
not even name capitolisation... I use ucwords().
I can't think of anything I format before entering in database.