Simple Question. I just cant remember the code.

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

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Simple Question. I just cant remember the code.

Post by tecktalkcm0391 »

How could I make the variable $test that is equal to 123456789 be changed to 123-45-6789.

Code: Select all

<?php

$test = '123456789';

//I want it to be changed to 123-45-6789

?>
I want to know how to do this because the value of test is comming from the user from a post on a form, and I need it to be in the form of XXX-XX-XXXX in order for it to match up with the records already in the database.

Thanks
Soogn
Forum Newbie
Posts: 11
Joined: Sat May 27, 2006 12:53 pm

Post by Soogn »

Hey,

Not the most agile solution perhaps, but this will work fine.

Code: Select all

<?php
// Subject
$test = '123456789';
$result = false;
$result = substr($test, 0, 3);
$result .= '-' . substr($test, 3, 5);
$result .= '-' . substr($test, 5, 9);
?>
Maybe my figure in substr() are wrong, it's late - but you get the idea!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

yeah it i had to change it a little to:

Code: Select all

<?php 
// Subject 
$test = '123456789'; 
$result = false; 
$result = substr($test, 0, 3); 
$result .= '-' . substr($test, 3, 2); 
$result .= '-' . substr($test, 5, 4); 


?>
but now it works

THANKS!
Soogn
Forum Newbie
Posts: 11
Joined: Sat May 27, 2006 12:53 pm

Post by Soogn »

Hey,

That was a lighting response, my bad on those numbers - didn't realise substr affected the soruce string too - glad to help :)
Post Reply