webwired Posted March 12, 2006 Share Posted March 12, 2006 Hi, I've tried several splitting techniques on php.net, but I can't seem to find one that will allow me to do something like this... Split this 4 number variable into 4 different variables, in order.$number = '3956';Such as this.$digit1 = '3';$digit2 = '9';$digit3 = '5';$digit4 = '6';Could anyone please help me? Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/ Share on other sites More sharing options...
sgb162 Posted March 12, 2006 Share Posted March 12, 2006 Need to use substr()Example:[code]<?php$number = "2345";echo $number . "<br>";$digit1 = substr($number,0,1);$digit2 = substr($number,1,1);$digit3 = substr($number,2,1);$digit4 = substr($number,3,1); echo $digit1 . "<br>"; echo $digit2 . "<br>";echo $digit3 . "<br>";echo $digit4 . "<br>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/#findComment-16542 Share on other sites More sharing options...
webwired Posted March 12, 2006 Author Share Posted March 12, 2006 Thank you, works like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/#findComment-16545 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2006 Share Posted March 12, 2006 Actually, in PHP5 there is a function that does thism [a href=\"http://www.php.net/str_split\" target=\"_blank\"]str_split[/a](). If you are still using PHP4, you can treat the string like sort of an array:[code]<?php$str = '2345';for ($i=0;$i<strlen($str);$i++) { $x = $i - 1; $var = 'digit' . $x; $$var = $str{$i};}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/#findComment-16552 Share on other sites More sharing options...
Barand Posted March 12, 2006 Share Posted March 12, 2006 [code]$x = $i + 1;[/code]would be an improvement ;-) Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/#findComment-16794 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2006 Share Posted March 12, 2006 oops! :-) Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/#findComment-16833 Share on other sites More sharing options...
txmedic03 Posted March 13, 2006 Share Posted March 13, 2006 You can even do $i++ instead of the $i = $i + 1. Not important, just a little side not for your own personal knowledge. kenrbnsn's example is excellent, overlooking the - 1. Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/#findComment-16919 Share on other sites More sharing options...
webwired Posted March 13, 2006 Author Share Posted March 13, 2006 After the four digit number is broken down into four separate numbers, can you then turn it into a letter as A=0, B=1 and so on? I thought this was supposed to work, but it didn't...[code]$digit1 = ($seg1digit1(Ord('A')));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4723-splitting-a-4-number-variable-into-4-different-variables/#findComment-17125 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.