note 67273 added to ref.array
| From: | vinaur at gmail dot com | Date: | Tue, 06 Jun 2006 20:36:01 +0000 |
| Subject: | note 67273 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-113373@lists.php.net to get a copy of this message | ||
Regarding the array to string (parse_line) and string to array (parse_array) functions posted below
by Kevin Law.
The functions will not work correctly if the array being parsed contains values that include commas
and possibly parentheses.
To solve this problem I added urlencode and urldecode functions and the result looks like this:
<?php
function parse_line($array){
$line = "";
foreach($array AS $key => $value){
if(is_array($value)){
$value = "(". parse_line($value) . ")";
}
else
{
$value = urlencode($value);
}
$line = $line . "," . urlencode($key) . ":" . $value . "";
}
$line = substr($line, 1);
return $line;
}
function parse_array($line){
$q_pos = strpos($line, ":");
$name = urldecode(substr($line,0,$q_pos));
$line = trim(substr($line,$q_pos+1));
$open_backet_pos = strpos($line, "(");
if($open_backet_pos===false || $open_backet_pos>0){
$comma_pos = strpos($line, ",");
if($comma_pos===false){
$result[$name] = urldecode($line);
$line = "";
}else{
$result[$name] = urldecode(substr($line,0,$comma_pos));
$result = array_merge($result, parse_array(substr($line,$comma_pos+1)));
$line = "";
}
}else if ($open_backet_pos==0){
$line = substr($line,1);
$num_backet = 1;
$line_char_array = str_split($line);
for($index = 0; count($line_char_array); $index++){
if($line_char_array[$index] == '('){
$num_backet++;
}else if ($line_char_array[$index] == ')'){
$num_backet--;
}
if($num_backet == 0){
break;
}
}
$sub_line = substr($line,0,$index);
$result[$name] = parse_array($sub_line);
$line = substr($line,$index+2);
}
if(strlen($line)!=0){
$result = array_merge($result, parse_array($line));
}
return $result;
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 75.4.151.146
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/note/edit/67273
Del: integrated -- http://master.php.net/note/delete/67273/integrated
Del: useless -- http://master.php.net/note/delete/67273/useless
Del: bad code -- http://master.php.net/note/delete/67273/bad+code
Del: spam -- http://master.php.net/note/delete/67273/spam
Del: non-english -- http://master.php.net/note/delete/67273/non-english
Del: in docs -- http://master.php.net/note/delete/67273/in+docs
Del: other reasons-- http://master.php.net/note/delete/67273
Reject -- http://master.php.net/note/reject/67273
Search -- http://master.php.net/manage/user-notes.php