Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2011, 07:54 PM   PM User | #1
coding_begins
Regular Coder

 
Join Date: Aug 2011
Posts: 134
Thanks: 20
Thanked 0 Times in 0 Posts
coding_begins is an unknown quantity at this point
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/..
coding_begins is offline   Reply With Quote
Old 11-23-2011, 08:00 PM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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($v1))); // outputs "/1/2/3/4/5/6"

Inigoesdr is offline   Reply With Quote
Users who have thanked Inigoesdr for this post:
coding_begins (11-23-2011)
Old 11-23-2011, 08:03 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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($num1"/"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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
coding_begins (11-23-2011)
Old 11-23-2011, 09:55 PM   PM User | #4
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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:
Code:
/1/1/2/9/9/1/2/
__________________
ZCE

Last edited by kbluhm; 11-23-2011 at 10:04 PM..
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 AM.


Advertisement
Log in to turn off these ads.