2
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";'

How can I convert it to an array?

5
  • 7
    First question is, how did you get such string? Commented Sep 26, 2013 at 10:17
  • 4
    Seems like a XY problem. Where do you get this string? Commented Sep 26, 2013 at 10:17
  • It is not easy to convert it to an array...how can we split this type of strings...?? try to pass this input with just values only.. Commented Sep 26, 2013 at 10:24
  • Agreed with other comments. This is a crazy format to be trying to import. If you can get the data in a more sensible format (eg JSON), it would be much easier to work with. Why is the data in this format in the first place? It looks like program code; what language is it written in? Why are you trying to import code from one language into another? Commented Sep 26, 2013 at 10:56
  • it's from javascript, there is a lot of data which store in javascript arrays. I tried to extract these data to store in mysql Commented Sep 27, 2013 at 8:07

6 Answers 6

1
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';

preg_match_all('/"(.*)";/U', $str, $matches);
$arr = $matches[1];

var_dump($arr);
Sign up to request clarification or add additional context in comments.

2 Comments

Will work fir sample above, but fail if there are other string literals (or may be HEREDOCS e.t.c.)
Depends whether the OP has any other variants
1

Use

preg_split

to select just the values and put it in to an array

example:

$keywords = preg_split("/[\s,]+/", $str);

Just change the pattern!

check the documentation: http://php.net/manual/en/function.preg-split.php

Comments

1
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';
$chunks = explode(';', $str);

for ($aa=0;$aa<count($chunks);$aa++)
{
    $chunks1 = explode('=', $chunks[$aa]);
    $myArray[]=$chunks1[1];
}

Comments

1

If you can add new lines after the semicolons (;), take a look at the parse_ini_string() function.

$str   = str_replace(';', "\n", $str); // <- add them in the source if you can
$array = parse_ini_string($str);

Comments

0

Something like this?

<?php
$str = '$answers[0] = "Love it"; $answers[1] = "Hate it."; $answers[2] = "Doesnt care"; $answers[3] = "Like it";';
eval($str);
print_r($answers);

?>

2 Comments

Don't use eval() if you don't have to. And there is always another way to achieve what you want to. Also, there is no $ signs in OP example string, so it's not an answer.
eval() is not a good practice..it allows permission to execute codes in the server for the input..that may harm your server.....so try another method ....
0
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care";   answers[3] = "Like it";';

preg_match_all('/"(.*?)"/', $str, $answers);

$answer = $answers[0];
print_r($answer);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.