Jump to content

Splitting up 4 numbers


master82

Recommended Posts

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] entered

var1 = [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...
Link to comment
Share on other sites

...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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.