Enjoy an ad free experience by logging in. Not a member yet?
Register .
11-23-2011, 07:54 PM
PM User |
#1
Regular Coder
Join Date: Aug 2011
Posts: 134
Thanks: 20
Thanked 0 Times in 0 Posts
splitting a number
this is probably simple...but can someone help me out.
i have variable $var=some number.the number can be of any length.examples 112,9912 etc.
i need to split each digit and seperate it by a /
For example,if $var=6066, I need to split it and store in a variable as /6/0/6/6/..
11-23-2011, 08:00 PM
PM User |
#2
Super Moderator
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Not sure if you intended to include that comma in the number or not, but this handles with/without comma:
PHP Code:
$numbers = array( '123456' , '123,456' ); foreach( $numbers as $v ) { $v = str_replace ( ',' , '' , $v ); var_dump ( '/' . implode ( '/' , str_split ( $v , 1 ))); // outputs "/1/2/3/4/5/6" }
Users who have thanked Inigoesdr for this post:
11-23-2011, 08:03 PM
PM User |
#3
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
There are several ways to accomplish this. I'm lazy, so I'd make use of wordwrap to force it.
PHP Code:
$num = 6066 ; $separated = wordwrap ( $num , 1 , "/" , true );
Super easy to do. You'll need to prepend and append the / if you want to keep it, but that's easy enough to do.
Users who have thanked Fou-Lu for this post:
11-23-2011, 09:55 PM
PM User |
#4
Senior Coder
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
This will remove all non-digits (commas, etc) as well as append/prepend slashes:
PHP Code:
$num = '1,129,912' ; $num = preg_replace ( array( '/\D/' , '//' ), array( '' , '/' ), $num ); echo $num ;
Output:
Last edited by kbluhm; 11-23-2011 at 10:04 PM ..
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 06:26 AM .
Advertisement
Log in to turn off these ads.