-4

I have a string like this:

$values = "[121],[622],[872]";

I would like to convert this into an array, I would like each number in the square brackets to be an array item.

any suggestions how I can do this?

1
  • Have you looked at functions like explode() and str_replace(); or preg_match()? Commented Feb 8, 2013 at 12:29

3 Answers 3

4
$array = explode(',',str_replace(array('[',']'),array('',''),$values));
Sign up to request clarification or add additional context in comments.

Comments

3

Seriously? Use explode()

$values = "[121],[622],[872]";
$values = str_replace(array("[", "]"), "", $values);
$values = explode(",", $values);

Or in a simpler way:

$array = explode("," ,str_replace(array("[", "]"), "", $values));

This gives values as:

Array
  121
  622
  872

4 Comments

Seriously? you read the question? You need a regex here before exploding, he just needs numbers...
@Mr.Alien Done, using simple str_replace.
took back the -1 too :)
LOL awarded to ikkez :p
0

See This

$values = "[121],[622],[872]";
$values = substr($values,1);
$values = substr($values,0,-1);
$arr = explode("],[",$values);

Hope this helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.