shamuraq Posted May 14, 2009 Share Posted May 14, 2009 Hi there, Anyone knows how i can split say eg, 5432 into 5, 4, 3 ,2? The explode() function still needs a delimiter to split meaning i would loose something. I need to split without losing anything.... Thanx in advance... Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/ Share on other sites More sharing options...
wildteen88 Posted May 14, 2009 Share Posted May 14, 2009 You can access each individual character within a string using this $string = '12345'; echo $string{3}; // get the 4th character echo $string{1}; // get the 2nd character echo $string{4}; // get the 5th character Alternatively you can use a loop: for($i = 0, $j = strlen($string); $i < $j; $i++) { echo $string{$i}; } Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834255 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 $array = str_split($string); Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834256 Share on other sites More sharing options...
shamuraq Posted May 14, 2009 Author Share Posted May 14, 2009 You can access each individual character within a string using this $string = '12345'; echo $string{3}; // get the 4th character echo $string{1}; // get the 2nd character echo $string{4}; // get the 5th character Alternatively you can use a loop: for($i = 0, $j = strlen($string); $i < $j; $i++) { echo $string{$i}; } so does that mean i can split them and store them as individual integer. Eg, 54321 into $a, $b,$c, $d, $e? Where $a = 5; $b = 4 etc... Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834260 Share on other sites More sharing options...
wildteen88 Posted May 14, 2009 Share Posted May 14, 2009 Yes that is achievable, however this method will only work with strings. Why are you wanting to store each digit within a separate variable? Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834273 Share on other sites More sharing options...
shamuraq Posted May 14, 2009 Author Share Posted May 14, 2009 Yes that is achievable, however this method will only work with strings. Why are you wanting to store each digit within a separate variable? Some math equation randomiser i need for a 3rd grader class... you know where they can learn about numbers with the position of ones, tens, hundreds and thousands. The class is a charitable cause for orphans and low income families. Since its a continuous effort i just need a script so that i can just print it on the run... Thanx again m8... Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834282 Share on other sites More sharing options...
shamuraq Posted May 14, 2009 Author Share Posted May 14, 2009 btw wildteen88, i tried your: for($i = 0, $j = strlen($string); $i < $j; $i++) { echo $string{$i}; } it doesn't work. just blank screen my code goes : <? $x1 = rand(1001,9999); for($i = 0, $j = strlen($x1); $i < $j; $i++) { echo $x1{$i}; } ?> Anything wrong? Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834287 Share on other sites More sharing options...
shamuraq Posted May 14, 2009 Author Share Posted May 14, 2009 when i tried the syntax from KingPhillip, same result: $array = str_split($x1); echo $array; Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834291 Share on other sites More sharing options...
wildteen88 Posted May 14, 2009 Share Posted May 14, 2009 Are for arrays you use a different syntax, $x1 = rand(1001,9999); for($i = 0, $j = count($x1)-1; $i < $j; $i++) { echo $x1[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834293 Share on other sites More sharing options...
Daniel0 Posted May 14, 2009 Share Posted May 14, 2009 FYI wildteen, the curly bracket notation is deprecated as of PHP 6, so it would be better to use the square bracket like you do with arrays. See: http://www.php.net/manual/en/language.types.string.php#language.types.string.substr Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834296 Share on other sites More sharing options...
shamuraq Posted May 14, 2009 Author Share Posted May 14, 2009 Are for arrays you use a different syntax, $x1 = rand(1001,9999); for($i = 0, $j = count($x1)-1; $i < $j; $i++) { echo $x1[$i]; } Still doesn't work... I'm really am sorry guys... I'm just a bit slow... Pls bear with me... Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834319 Share on other sites More sharing options...
Brian W Posted May 14, 2009 Share Posted May 14, 2009 wow, you make that look hard... lol <?php $x1 = (string) rand(1001, 9999);//lowest number, highest number $digits = str_split($x1);//spilits it into digits echo "The number $x1<br><br>"; $places = array("Ones", "Tens", "Hundreds", "Thousands", "Ten thousands", "Hundred thousands"); foreach($digits as $place=>$digit){ echo $places[$place]." is ".$digit."<br>"; } ?> Have fun with that one, let me know if you want it to work differently. The educational system needs more interaction with computers. Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834332 Share on other sites More sharing options...
wildteen88 Posted May 14, 2009 Share Posted May 14, 2009 Sorry I mis read your code earlier. I thought you was using range at the time. rand() returns a number between 1001 and 9999. To break the number into ones, tends, hundreds, thousands I came up with this: <?php $x1 = (string) rand(1000, 9999); $fields = array('ones', 'tens', 'hundreds', 'thousands'); $digits = str_split($x1); echo '<p>The number: '. number_format($x1, ',') .'</p>'; ?> <table border="1" cellspacing="1" cellpadding="10"> <tr> <?php foreach($fields as $field) { echo '<th>'.ucwords($field).'</th>'; } ?> </tr> <tr> <?php foreach(array_reverse($digits) as $num) { echo '<td>'.$num.'</td>'; } ?> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834341 Share on other sites More sharing options...
Brian W Posted May 14, 2009 Share Posted May 14, 2009 lol, I noticed mine is broken anyways. its returning them in the wrong order... Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834343 Share on other sites More sharing options...
shamuraq Posted May 14, 2009 Author Share Posted May 14, 2009 nonetheless u guys rock. I really appreciate it man. Quote Link to comment https://forums.phpfreaks.com/topic/158161-solved-split-string/#findComment-834375 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.