Page 1 of 1

Simple Question. I just cant remember the code.

Posted: Sat May 27, 2006 4:10 pm
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

Posted: Sat May 27, 2006 4:16 pm
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!

Posted: Sat May 27, 2006 4:26 pm
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!

Posted: Sat May 27, 2006 4:29 pm
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 :)