Jump to content


Photo

Splitting up 4 numbers


  • Please log in to reply
8 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 30 August 2006 - 02:39 PM

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?

eg

9172 entered

var1 = 9
var2 = 1
var3 = 7
var4 = 2

Can this be done - and if so, how?

Thanks in advance...

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 August 2006 - 02:40 PM

Use substr(). http://www.php.net/m...tion.substr.php

Info: PHP Manual


#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 02:43 PM

.. and on the page Ober cites, the example #1 code shows exactly how you could do what you ask.
Legend has it that reading the manual never killed anyone.
My site

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 August 2006 - 02:48 PM

Yeah... you could use the curly brace method as well.

Info: PHP Manual


#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 August 2006 - 03:09 PM

...or, you could just treat the string as an array:
<?php
$string = "Test String";
for ($i = 0; $i < strlen($string); $i++) echo "{$string[$i]}<br />\n";
?>

so, for your example, i'd do something like this:
<?php
$string = "9172";
for ($i = 1; $i <= strlen($string); $i++) {
	eval("\$var$i = \$string[\$i - 1];");
}
?>

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 30 August 2006 - 03:11 PM

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

#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 August 2006 - 03:14 PM

try my edited post above... that produces $var1, $var2, etc... containing the 4 digits
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#8 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 30 August 2006 - 03:20 PM

It works! Thank you so much to everyone who helped!!!!!!

#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 August 2006 - 03:39 PM

one other note... some people on here would frown upon using eval() for something like this, so you could also do it like this:
<?php
$string = "9172";
for ($i = 1; $i <= strlen($string); $i++) {
	${"var$i"} = $string[$i - 1];
}
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users