-3

I want to create two variables from one string in PHP.

e.g.

$string = "2,50";

to

$a = "2";
$b = "50";

How to do that?

I want to use it to <sup> the cents in the price and style it differently, but I have this number as a whole. So I need to devide it into two pireces.

the format of the string is always with two decimal points and a comma as a delimiter, os this should be a little help.

Any idea how to do this using PHP?

IMPORTANT! I need to do that without using arrays in PHP!

3
  • Uh, what's wrong with arrays?
    – jszobody
    Nov 14, 2013 at 14:22
  • @jszobody Ask my teacher. Nov 14, 2013 at 14:24
  • Oh. So it's homework. Bah. Voting to close.
    – jszobody
    Nov 14, 2013 at 14:25

2 Answers 2

3

You can use the list construct along with explode() to achieve this:

list($a, $b) = explode(',', trim($string));

Demo.

3
  • WOw, thats nice. I will try it ;D Nov 14, 2013 at 14:20
  • It's working great. I will tick your answer in .. it says 7 minutses ;) Nov 14, 2013 at 14:24
  • @user2989475: This solution still uses an array given by explode. list just makes the assignment a little bit easier. If you really want to do this without arrays, then you may want to use a combination of strpos() and substr(). Nov 14, 2013 at 14:25
1

Use preg_split() this will give you an array with two entries, add the values of those entries to your original variables.

Not the answer you're looking for? Browse other questions tagged or ask your own question.