Welcome Guest, Not a member yet? Register   Sign In
Formatting Number with Regular Expression
#4

[eluser]pickupman[/eluser]
Ah...you asked for 10 digit strings first time. I am not a regex wiz but a while loop may work easier as it would work for any length of string
Code:
$input = '1234567111890';
$output = '';

while(strlen($input) > 0){
    $output .= substr($input, 0, 3) . '-'; #take first 3 digits
    $input = substr($input, 3); #remove those 3 digits from string
}
$output = rtrim($output, '-'); #remove trailing -
var_dump($output);
unset($input); #tidy up

You may want to test if $input is numeric if you didn't want any alpha characters.


Messages In This Thread
Formatting Number with Regular Expression - by El Forum - 03-03-2011, 08:56 AM
Formatting Number with Regular Expression - by El Forum - 03-03-2011, 09:05 AM
Formatting Number with Regular Expression - by El Forum - 03-03-2011, 08:40 PM
Formatting Number with Regular Expression - by El Forum - 03-03-2011, 10:52 PM
Formatting Number with Regular Expression - by El Forum - 03-04-2011, 12:07 AM



Theme © iAndrew 2016 - Forum software by © MyBB