php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75639 string2array($str)
Submitted: 2017-12-06 11:29 UTC Modified: 2017-12-06 14:22 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: web at houhejie dot cn Assigned:
Status: Closed Package: *General Issues
PHP Version: Next Major Version OS:
Private report: No CVE-ID: None
 [2017-12-06 11:29 UTC] web at houhejie dot cn
Description:
------------
---
From manual page: http://www.php.net/ref.strings
---
hope have a function string2array($str) to convert a arrayString to Array, like:"[1,[2-1,2-2]]". we can't use the split() or explode(), although the eval() can do it , but that's very dangerous for uncontrolled data.

Test script:
---------------
$arr=string2array("[1,[2-1,2-2]]");

Expected result:
----------------
$arr=[1,[2-1,2-2]]


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-06 11:40 UTC] spam2 at rhsoft dot net
that's why json exists

http://at2.php.net/manual/en/function.json-decode.php
http://at2.php.net/manual/en/function.json-encode.php
 [2017-12-06 11:45 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2017-12-06 11:45 UTC] requinix@php.net
$arr=[1,[2-1,2-2]]
would presumably be
  $arr=[1,[1,0]]
which requires evaluation and, like you said, that's dangerous. Plus this is a very specific request with very specific use cases, and there's so much room for flexibility that one function alone might not be enough.

As spam2 said, the string is close to JSON so that might work for you.

The traditional approach to this is to write a parser, with or without tools to assist you. If you're confident with regular expressions (or know someone who is) then you could use a regex to validate the string before eval(), which is obviously still risky. But using a different serialization format might be best - if that's an option for you.
 [2017-12-06 14:22 UTC] web at houhejie dot cn
-Status: Wont fix +Status: Closed
 [2017-12-06 14:22 UTC] web at houhejie dot cn
OK,spam2 is right. I forgot it. json_decode() works well~ thanks~
and I add a note to json_decode() and book.array, hope add this to array list to help others:)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC