0

I have a string, which is comes from js using encodeURICompoent(). Now i would like to convert this string into array. I tried my level best to solve this. But i couldn't find any solution to do this. Any help greatly appreciated.

array string = 'array(0=>array("packid"=>22,"pverid"=>18,"yaml"=>"- url: /static
 static_dir: static

- url: .*
  script: provider.py" ),1=>array("packid"=>23,"pverid"=>19,"yaml"=>"- url: /static
  static_dir: static

- url: .*
  script: provider.py" ));';

Thanking you, sureace

deceze
  • 510,633
  • 85
  • 743
  • 889
user400091
  • 67
  • 2
  • 6
  • What do you want to extract out of those values? You need to use `implode` to convert string to array. – Sarfraz Jul 23 '10 at 09:45
  • 1
    It's totally unclear what your string is. So far you have said that the string is JSON.stringified, in YAML format, URL encoded and in PHP array format. **What is it?!** The question is impossible to answer. – deceze Jul 23 '10 at 10:38
  • What i want is, I am getting json objects from client side to php, which contains encoded data string using javascript encodeURIComponent(). If it contains this data, i am unable to convert this object into array using json_decode() function. jsonstring="{\"packid\":\"22\",\"pverid\":\"18\",\"yaml\":\"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.*%0A%20%20script%3A%20provider.py\"},{\"packid\":\"23\",\"pverid\":\"19\",\"yaml\":\"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.*%0A%20%20script%3A%20provider.py\"}" Thanking you – user400091 Jul 23 '10 at 11:00
  • 1
    *Why* are you mixing JSON with YAML with PHP arrays? – NullUserException Jul 23 '10 at 11:33
  • possible duplicate of [Safely convert user-provided array string into PHP array?](http://stackoverflow.com/q/2143004/), [Convert PHP array string into an array](http://stackoverflow.com/q/684553/90527) – outis Feb 24 '12 at 05:59

6 Answers6

1

It looks like eval should do the job of turning this string into an array.
BUT: You should never, ever use eval, especially with input that you get from external sources, since it opens up a slew of security issues. Instead, you should parse the string by hand, but that's going to be quite a nuisance.

You should find a better way to send the array values, for example via JSON. Especially when sending values from Javascript, this is the usual method.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • doesn't look that way to me.. looks like it would throw an exception at best. Looks like a good bit of the string is missing. – Dagg Nabbit Jul 23 '10 at 09:51
  • @no I think those newlines are part of the string. – deceze Jul 23 '10 at 09:52
  • When i am trying look like this, it throws parse errors. $packs = eval($post["packs"]); print_r($packs); Errors ----------- Parse error: syntax error, unexpected $end in eval()'d code on line 9 – user400091 Jul 23 '10 at 10:11
  • I am using JSON.stringify() jquery plug in method at client side to sending json data. – user400091 Jul 23 '10 at 10:46
1

As mentioned by deceze, you can use eval() to turn this string into a PHP array, but whether you should or not is another matter...

$myCodeString = 'array(0=>ar ...... etc.';  // Your coded string

eval('$myArray = '.$myCodeString);
var_dump($myArray);

Which at least returns a valid PHP array:

array(2) {
  [0]=>
  array(3) {
    ["packid"]=>
    int(22)
    ["pverid"]=>
    int(18)
    ["yaml"]=>
    string(71) "- url: /static
 static_dir: static

- url: .*
  script: provider.py"
  }
  [1]=>
  array(3) {
    ["packid"]=>
    int(23)
    ["pverid"]=>
    int(19)
    ["yaml"]=>
    string(72) "- url: /static
  static_dir: static

- url: .*
  script: provider.py"
  }
}

You probably want to parse your "yaml" fields some more...?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • "you can use eval()" Don't do it. – NullUserException Jul 23 '10 at 11:30
  • Thanks for stressing the point, but I wasn't necessarily saying you should. There seemed to be some debate as to whether this was even technically possible. – MrWhite Jul 23 '10 at 12:35
  • This worked great for me. The security issue is only a factor if you don't have control over the data. In my case the array is auto generated and only exposed internal. No need to be paranoid. – Max Kielland Jan 08 '11 at 21:05
0

Have a look at the implode function.

cypher
  • 6,822
  • 4
  • 31
  • 48
0

it would be easier to use serialize or json for transferring an array. (but in that case, you would need a little function on clientside for serializing / json_encode)

EDIT: if encodeURIComponent is you problem, just do a urldecode before json_decode...

oezi
  • 51,017
  • 10
  • 98
  • 115
0

It's very hard to understand what you are asking here.

Do you want to convert a yaml string to an php array? If so, try spyc.

alexn
  • 57,867
  • 14
  • 111
  • 145
  • yes, user always giving yaml data in textarea at client side. i am always getting that yaml data and store that exact yaml data(with new lines, spaces...) in database for future purposes. Can u give some idea to solve this problem. – user400091 Jul 23 '10 at 10:16
-1

yes i tried, but it dosen't solve my problem.

What i want is, I am getting json objects from client side to php, which contains encoded data string using javascript encodeURIComponent(). If it contains this data, i am unable to convert this object into array using json_decode() function.

json string=packs="{\"packid\":\"22\",\"pverid\":\"18\",\"yaml\":\"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.%0A%20%20script%3A%20provider.py\"},{\"packid\":\"23\",\"pverid\":\"19\",\"yaml\":\"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.%0A%20%20script%3A%20provider.py\"}"

Thanking you, sureace

user400091
  • 67
  • 2
  • 6