master82 Posted August 30, 2006 Share Posted August 30, 2006 In a form I have, a user inputs a 4 numerical value into a text box.how would I go about splitting this into 4 variables?[u]eg[/u][b]9172 [/b] enteredvar1 = [b]9[/b]var2 = [b]1[/b]var3 = [b]7[/b]var4 = [b]2[/b]Can this be done - and if so, how?Thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/ Share on other sites More sharing options...
ober Posted August 30, 2006 Share Posted August 30, 2006 Use substr(). http://www.php.net/manual/en/function.substr.php Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82856 Share on other sites More sharing options...
AndyB Posted August 30, 2006 Share Posted August 30, 2006 .. and on the page Ober cites, the example #1 code shows exactly how you could do what you ask. Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82857 Share on other sites More sharing options...
ober Posted August 30, 2006 Share Posted August 30, 2006 Yeah... you could use the curly brace method as well. Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82861 Share on other sites More sharing options...
obsidian Posted August 30, 2006 Share Posted August 30, 2006 ...or, you could just treat the string as an array:[code]<?php$string = "Test String";for ($i = 0; $i < strlen($string); $i++) echo "{$string[$i]}<br />\n";?>[/code]so, for your example, i'd do something like this:[code]<?php$string = "9172";for ($i = 1; $i <= strlen($string); $i++) { eval("\$var$i = \$string[\$i - 1];");}?>[/code]good luck Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82890 Share on other sites More sharing options...
master82 Posted August 30, 2006 Author Share Posted August 30, 2006 Sorry, I've tried to follow those examples but i still cant seem to split up the the variable $value into the four I need.Any chance of putting an example on here using $value as the 4 digit value of 2468?Sorry to ask - still trying to learn Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82893 Share on other sites More sharing options...
obsidian Posted August 30, 2006 Share Posted August 30, 2006 try my edited post above... that produces $var1, $var2, etc... containing the 4 digits Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82900 Share on other sites More sharing options...
master82 Posted August 30, 2006 Author Share Posted August 30, 2006 It works! Thank you so much to everyone who helped!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82910 Share on other sites More sharing options...
obsidian Posted August 30, 2006 Share Posted August 30, 2006 one other note... some people on here would frown upon using eval() for something like this, so you could also do it like this:[code]<?php$string = "9172";for ($i = 1; $i <= strlen($string); $i++) { ${"var$i"} = $string[$i - 1];}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19157-splitting-up-4-numbers/#findComment-82929 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.