_tab_to_str("", $TAB); } function _tab_to_str($pre, $TAB) { $content = ""; if($TAB) { foreach($TAB as $key => $value) { if(is_array($value)) $content .= $this->_tab_to_str($pre.strlen($key).":".$key, $TAB[$key]); else $content .= $pre.strlen($key).":".$key.strlen($value).":".$value."\n"; } } else return $pre."a:\n"; return $content; } function str_to_tab($content) { $TAB = array(); $n = 0; while($n < strlen($content)) { $LINE_FOUND = 0; $words = array(); while($n < strlen($content) && !$LINE_FOUND) { $length = ""; $word = ""; $ENDS_WITH_EMPTY_TAB = 0; while($n < strlen($content) && strcmp(substr($content, $n, 1), ":") != 0) { $length .= $content[$n]; $n++; } $n++; if(strcmp($length, "a") == 0) $ENDS_WITH_EMPTY_TAB = 1; else { for($k = 0; $k < $length; $k++) { $word .= $content[$n]; $n++; } $words[] = $word; } $LINE_FOUND = $ENDS_WITH_EMPTY_TAB || $n == strlen($content) || strcmp(substr($content, $n, 1), "\n") == 0; } $n++; if($words) $TAB = $this->affect($TAB, $words, $ENDS_WITH_EMPTY_TAB); } return $TAB; } function affect($TAB, $words, $ENDS_WITH_EMPTY_TAB) { if(count($words) > ($ENDS_WITH_EMPTY_TAB ? 1 : 2)) { if(!isset($TAB[$words[0]]) || !is_array($TAB[$words[0]])) $TAB[$words[0]] = array(); $_words = array(); $FIRST = 1; foreach($words as $word) { if($FIRST) $FIRST = 0; else $_words[] = $word; } $TAB[$words[0]] = $this->affect($TAB[$words[0]], $_words, $ENDS_WITH_EMPTY_TAB); } else $TAB[$words[0]] = ($ENDS_WITH_EMPTY_TAB ? array() : $words[1]); return $TAB; } function str_to_hex($str) { $hex = ""; $n = 0; while($n < strlen($str)) { $hex_char = dechex(ord($str[$n])); if(strlen($hex_char) == 1) $hex_char = "0".$hex_char; $hex .= $hex_char; $n++; } return $hex; } function hex_to_str($hex) { $n = 0; $str = ""; while($n + 2 <= strlen($hex)) { $str .= urldecode("%".substr($hex, $n, 2)); $n += 2; } return $str; } function request_tab($str) { return $this->str_to_tab($this->hex_to_str(utf8_decode($str))); } function response_str($TAB) { return $this->str_to_hex($this->tab_to_str($TAB)); } } ?>