Jump to content

string to array

- - - - -

  • Please log in to reply
7 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
hi guys

i want change string to array

this is my string
$str = '[1]=Array("-","x","1","4","r, 2x","2x2","x3");';

how to change to array?

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
The best solution would be to use some regex, but since I'm not good in regex, I'll give you the next best solution

First with substr remove the start "[1]=Array(" and the end ");" if the string
Than use str_replace to replace all " with nothing
And use explode with the delimiter "," to split everything into an array

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Parsing, RegEx, or maybe even:
$string = '[1]=Array("-","x","1","4","r, 2x","2x2","x3");';
$array = array();
eval("\$array$string");
print_r($array[1]);

However these solutions are only dirty hacks, are you able to serialize the original array so that it can be unserialized in your program?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
thank you Alexander,
serialize?

this code is javascript, and i parse/get code with php of another site.

and i need change to php.

can you more help me?

thank you so much

#5
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
show us your php code that you need to change

#6
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
abc[1]=Array(91821,"John","2009-02-17",287,"0401");
This is my javascript code

and i get this code with php, i want change to array, and with full secure.

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
What you would want to do in this case, is serialize (arrange in to series) an array, so that it can be parsed as a string and passed to PHP with the benefit of being able to parse it back in to an array.

Off the top of my head you can use JSON (javascript object notation) to encode the array for sending, this can be decoded by PHP's json_decode() functions. provided your PHP was compiled with them. You will require a small external library to implement this, such as json.js, jQuery, or similar.

or

Possibly implement a php.js project serialize clone (http://phpjs.org/packages/configure/) you can set preset to none, and then check fhe "serialize" function, and at the bottom name and compile it.

This would allow you to include the file, and use serialize(the_array) and then in PHP, you can use its native unserialize(the_string)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
nice

thank you alex.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users