-2

Possible Duplicate:
How can I split a comma delimited string into an array in PHP?
extract tags (words) from whole string

I'm trying to read a long variable into an array.

Variable $x = "text1", "text2", "text3", etc...

$x = '"text1", "text2", "text3"';

Do I just call it into my array like:

array($x)?

This doesn't seem to work

I want the final product to be:

array("text1", "text2", "text3")

What am I doing wrong? Thanks!

EDIT:

Ok, some specifics:

I'm trying to replace the date fields in the following array with many dynamically read dates:

$myCalendar->setSpecificDate(array("2012-09-09", "2012-09-10"), 1, '');

I have the dynamically read dates in a variable $x. When I echo $x, I get something in the format "2012-09-09", "2012-09-10", "2012-09-09", "2012-09-10", "2012-09-09", "2012-09-10", etc

Thanks

Community
  • 1
  • 1
user1569034
  • 47
  • 1
  • 2
  • 9

3 Answers3

2

If your variable is a string like:

$x = '"text1", "text2", "text3"'

You can convert it to an array with str_getcsv like the following:

$x = '"text1", "text2", "text3"';
$yourArray = str_getcsv($x);
Brett
  • 2,502
  • 19
  • 30
1

If your input string is '"text1", "text2", "text3"'

You can use the following code to get an array with three string

print_r( 
   explode( ",", preg_replace('/[" ]*/', "", '"text1", "text2", "text3"'))
);
// Outputs Array ( [0] => text1 [1] => text2 [2] => text3 )

This assumes there are no spaces in your strings, you'll have to play with it if spaces are allowed

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
0

Explode it. String becomes an array based on a delimiter.

$x = "text1,text2,text3";
$new_array = explode(',', $x);

This becomes:

Array
(
    [0] => text1
    [1] => text2
    [2] => text3
)
wesside
  • 5,622
  • 5
  • 30
  • 35