How to divide a variable into an array

Asked

Viewed 184 times

1

My question is a bit complex, for which I will try to explain.

I have a php variable, which always shows results as follows:

"8017990310062\r8017990142064\r801792340068\r"

What I intend to do is "tidy" the displayed numbers of the variable and assign in an array, that is, first I intend to take the quotes of the variable, then remove the r and then take the numbers and each array has a number.

Example:

$array[0] = 8017990310062;
$array[1] = 8017990142064;
$array[2] = 801792340068;

Note that my variable may have more than 3 values, for example 5, 7 or 8.

How can I do this?

  • 1

    Try it out there: explode('\r', $var); If I understand correctly, I think you’ll get what you want. A $var is your whole string

  • I think that’s right, however how do I first remove the "quotes" of the variable result?

  • 1

    You have an answer already down that does what you want Gonçalo

1 answer

2


Do so:

$var = "8017990310062\r8017990142064\r801792340068\r";

$varRep = str_replace('"', '', $var);
$varExp = explode('\r', $varRep);
  • 1

    That’s right, thanks for the help, in 5 minutes I accept the answer!

  • 2

    It’s been five minutes...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.