Req #75639 [Wfx->Csd]: string2array($str)
| From: | web at houhejie dot cn | Date: | Wed, 06 Dec 2017 14:22:10 +0000 |
| Subject: | Req #75639 [Wfx->Csd]: string2array($str) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-212970@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75639&edit=1
ID: 75639
User updated by: web at houhejie dot cn
Reported by: web at houhejie dot cn
Summary: string2array($str)
-Status: Wont fix
+Status: Closed
Type: Feature/Change Request
Package: *General Issues
PHP Version: Next Major Version
Block user comment: N
Private report: N
New Comment:
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:)
Previous Comments:
------------------------------------------------------------------------
[2017-12-06 11:45:04] 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 11:40:02] 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:29:11] 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]]
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75639&edit=1