Tiltan Posted April 10, 2009 Share Posted April 10, 2009 Ok i dident know how to name this topic :-\ Lets say a form is submited (So i dont know what the numbers will be...) xxx * xxxxx = x'xxx'xxx'xxx Lets just say: 3'496'562'654 How can i get the result to be put like this: 654 562 496 3 I want the result to be cut from behind every 3rd numeral, no matter what the result is. Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/ Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Just an idea : $number = "333'333'333"; $number = explode("'", $number); foreach($number as $key => $value) { $position = $key + 1; $position = $position / 3; if(is_int($position)) { echo $value.'<br />'; } else { echo $value; } } Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806480 Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 This is presuming there is a ' where it needs to be split. If this cannot be guaranteed then the best way would be to write a loop to work backwards splitting it into chunks. Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806623 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Meh. He put quotations in the original code - I just worked with that. It wouldn't be too hard to do it without though. Instead of exploding it, just reverse it (not sure about ints, but for strings you can use strrev() and then something like this maybe? $number = 243423; $length = strlen($number); for($i = 0; $i <= $length; $i++) { $key = ceil($i / 3); $array[$key] .= substr($number, $i, 1); } foreach($array as $key => $value) { echo $value.'<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806683 Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 This is the best I can do... $number='1234567890'; $arrSplit=splitNumber($number); function splitNumber($num) { $revNumber=revstr($num); $len=strlen($num); $tmp=''; for ($i=0;$i<$len;++$i) { $tmp.=$revNumber[$i]; if ($i>1&&$i%3==0) {$tmp.='*';} } return explode('*',revstr($tmp)); } Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806695 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Oh man I never knew the % operator - I just googled it. That makes stuff a lot easier Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806702 Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 Having to re-post as I just tried my code and got the strrev() messed up and the last item had 4 digits. $number=1234567890; $arrSplit=splitNumber($number); var_dump($arrSplit); function splitNumber($num) { $revNumber=strrev($num); $tmp=''; for ($i=0;$i<strlen($num);++$i) { $tmp.=$revNumber[$i]; if ($i>1 && ($i+1)%3 == 0) {$tmp.='*';} } return explode('*',strrev($tmp)); } Output from var_dump(): array(4) { [0]=> string(1) "1" [1]=> string(3) "234" [2]=> string(3) "567" [3]=> string(3) "890" } Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806709 Share on other sites More sharing options...
Daniel0 Posted April 10, 2009 Share Posted April 10, 2009 You're all making it needlessly more complex that it has to be. $parts = explode("'", number_format($number, 0, '.', "'")); Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806717 Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 Now THAT'S optimisation Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806741 Share on other sites More sharing options...
Tiltan Posted April 10, 2009 Author Share Posted April 10, 2009 Ok great, atleast you know what i mean But i am guite new to this with php. I tried to use jackpf's code $number = 243423; $length = strlen($number); for($i = 0; $i <= $length; $i++) { $key = ceil($i / 3); $array[$key] .= substr($number, $i, 1); } foreach($array as $key => $value) { echo $value.'<br />'; } If the $number = 1232766 It will split it up to 766 and 232 and 1. Great thats exactly what i wanted. Now. How can i get them in to $a, $b and $c for example, depending on how many parts. There could be like 1 to 10 parts. (this is were my lack of knowledge kicks in... (again)) And this... You're all making it needlessly more complex that it has to be. $parts = explode("'", number_format($number, 0, '.', "'")); works aswell. But i still cant use the parts :,( i used foreach($parts as $key => $value) { echo $value.'<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806753 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 You're all making it needlessly more complex that it has to be. $parts = explode("'", number_format($number, 0, '.', "'")); Lol - it's fun though. And I'm not sure exactly what you mean by "put them into $a $b and $c"... Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806760 Share on other sites More sharing options...
Daniel0 Posted April 10, 2009 Share Posted April 10, 2009 You're all making it needlessly more complex that it has to be. $parts = explode("'", number_format($number, 0, '.', "'")); works aswell. But i still cant use the parts :,( i used foreach($parts as $key => $value) { echo $value.'<br />'; } foreach(explode("'", number_format(1234567890, 0, '.', "'")) as $key => $value) { echo $value . PHP_EOL; } outputs 1 234 567 890 What else do you want? If you want it reversed then just do: $parts = explode("'", number_format(1234567890, 0, '.', "'")); for ($i = sizeof($parts) - 1; $i >= 0; $i--) { echo $parts[$i] . PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806765 Share on other sites More sharing options...
Tiltan Posted April 10, 2009 Author Share Posted April 10, 2009 OK thx, Got it now Here is what i created. Completly useless but still fun. Feel free to try it. The result will allways be 999 Game with 999 <form method="post" action="math.php"> <table> <tr><input type="hidden" name="calc" value="calc"> <td><input type="hidden" size="10" maxlength="10" name="a" value="999">999 *</td> <td><input type="text" size="10" maxlength="10" name="b" value=""></td> <td><input type="submit" value="submit" name="submit"></td> </tr> </table> </form> <?php if(isset($_POST['calc'])){ $a = $_POST['a']; $b = $_POST['b']; echo $a .' * '. $b .''; $number = $a * $b; echo ' = '.$number .'<br/><br/>'; $parts = explode("'", number_format($number, 0, '.', "'")); foreach($parts as $key => $value) { echo '+ '. $value.'<br />'; } $s = $parts[0] + $parts[1] + $parts[2] + $parts[3] + $parts[4] + $parts[5]; if($s == 999){ echo '= '. $s; } else{ echo '= '. $s; echo '<br />'; while($s > 999){ $s = $parts = explode("'", number_format($s, 0, '.', "'")); foreach($s as $key => $value) { echo '+ '. $value.'<br />'; } $s = $s[0] + $s[1] + $s[2] + $s[3] + $s[4] + $s[5]; } echo '= '. $s; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806784 Share on other sites More sharing options...
Tiltan Posted April 10, 2009 Author Share Posted April 10, 2009 What i ment with the $a $b $c part How can i get them in to $a, $b and $c for example, depending on how many parts. There could be like 1 to 10 parts. (this is were my lack of knowledge kicks in... (again) Is this foreach($s as $key => $value) { echo '+ '. $value.'<br />'; } $s = $s[0] + $s[1] + $s[2] + $s[3] + $s[4] + $s[5]; } But would it be possibly to get it dynamicly? Now i just created 5. knowing that if they re empty they wont count. But there are other situations... Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806805 Share on other sites More sharing options...
Daniel0 Posted April 10, 2009 Share Posted April 10, 2009 Like you just did in the loop. Just iteratively keep adding. Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806808 Share on other sites More sharing options...
Tiltan Posted April 10, 2009 Author Share Posted April 10, 2009 i locked the form to 10 numbers. But if it would be 30 numbers long. It could look like this 999 999 999 999 999 999 999 999 clearly $s = $s[0] + $s[1] + $s[2] + $s[3] + $s[4] + $s[5]; wouldent be enough since there are 8 parts, not 6 What i would like to know if there is a way to create enough "$s[0]" automaticly, instead of guessing? Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806814 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 foreach($s as $key => $value) { $s += $s[$key]; } maybe... Quote Link to comment https://forums.phpfreaks.com/topic/153491-solved-slicing-a-number-s/#findComment-806820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.