how split number 3 parts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
srdva59
Forum Commoner
Posts: 77
Joined: Sun Feb 15, 2009 10:58 am

how split number 3 parts

Post by srdva59 »

hi,
i have a number with 9 digits lenght
and i want split this number: 922888222
into this: 922 888 222
how can i do that?
thanks a lot for your help :)
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: how split number 3 parts

Post by Mark Baker »

$number = 922888222;
$numberBits = str_split($number,3);
srdva59
Forum Commoner
Posts: 77
Joined: Sun Feb 15, 2009 10:58 am

Re: how split number 3 parts

Post by srdva59 »

hi thanks for your fast feedback but i receive a error: Call to undefined function: str_split() in
may be that function dont work in php4? :x
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how split number 3 parts

Post by jackpf »

I don't think so.

Read the comments here: http://uk.php.net/str_split

...or just upgrade :/
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: how split number 3 parts

Post by mayanktalwar1988 »

Code: Select all

 
<?
$string = "mayanktalwar1988";
$split_length = 3;
 
if(!function_exists('str_split')){
   function str_split($string,$split_length){
 
       $cnt = strlen($string);
 
       for ($i=0; $i < $cnt; $i += $split_length)
           $chunks[]= substr($string,$i,$split_length);    
   
       return $chunks;
   }
}
 
 
$chunks = str_split($string,$split_length);
 
foreach ($chunks as $chunk) {
echo $chunk . " ";
} 
?>
you can do like above if you dont have php5
yes str_split is a php5 specific function...uprade to php5 to use that
Post Reply