Hi,
I'm trying to format some $_POST info a certain way. The thing I get from $_POST is a 33 digit number, e.g.
$billing_code = 333333333333333333333333333333333.
I need to add dashes to the above number so that it looks like this:
$formatted_billing_code = 333-33333-3333-333333-333333-3333-33333
Can anyone tell me a quick way to do this? I was messing around with ereg_replace but I couldn't make it work.
Thanks!
Elise
regex problem - adding dashes to long number?
Moderator: General Moderators
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
Well, you could always go...
Messy, but hey.
Code: Select all
<?php
$i = 333333333333333333333333333333333;
$code =
substr($i,0,3).'-'.
substr($i,3,5).'-'.
substr($i,8,4).'-'.
substr($i,12,6).'-'.
substr($i,18,6).'-'.
substr($i,24,4).'-'.
substr($i,28,5);
?>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
$formatted = preg_replace('/^\d{3}\d{5}\d{4}\d{6}\d{6}\d{4}\d{5}$/', "$1-$2-$3-$4-$5-$6-$7", $number);Still think bennetman's way is good enough mind you
-
Bennettman
- Forum Contributor
- Posts: 130
- Joined: Sat Jun 15, 2002 3:58 pm
See, told you mine was messy. ^_-
Anyway, wouldn't that be:
with the ^ moved past the first /, and the escape slashes for \d?
Edit: XD we've both been fixing up mistakes XD
Anyway, wouldn't that be:
Code: Select all
$formatted = preg_replace("/^\d{3}\d{5}\d{4}\d{6}\d{6}\d{4}\d{5}$/", "$1-$2-$3-$4-$5-$6-$7", $code);Edit: XD we've both been fixing up mistakes XD
Last edited by Bennettman on Mon Apr 11, 2005 2:41 pm, edited 2 times in total.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia