http_chunked_decode
(PECL pecl_http >= 0.1.0)
http_chunked_decode — Decode chunked-encoded data
Description
string http_chunked_decode
( string
$encoded
)Decodes a string which is HTTP-chunked encoded.
Parameters
-
encoded
-
chunked encoded string
Return Values
Returns the decoded string on success or FALSE
on failure.
Examples
Example #1 A http_chunked_decode() example
<?php
$string = "".
"05\r\n".
"this \r\n".
"07\r\n".
"string \r\n".
"12\r\n".
"is chunked encoded\r\n".
"01\n\r\n".
"00";
echo http_chunked_decode($string);
?>
The above example will output:
this string is chunked encoded
[an error occurred while processing the directive]
- http_cache_etag
- http_cache_last_modified
- http_chunked_decode
- http_deflate
- http_inflate
- http_build_cookie
- http_date
- http_get_request_body_stream
- http_get_request_body
- http_get_request_headers
- http_match_etag
- http_match_modified
- http_match_request_header
- http_support
- http_negotiate_charset
- http_negotiate_content_type
- http_negotiate_language
- ob_deflatehandler
- ob_etaghandler
- ob_inflatehandler
- http_parse_cookie
- http_parse_headers
- http_parse_message
- http_parse_params
- http_persistent_handles_clean
- http_persistent_handles_count
- http_persistent_handles_ident
- http_get
- http_head
- http_post_data
- http_post_fields
- http_put_data
- http_put_file
- http_put_stream
- http_request_body_encode
- http_request_method_exists
- http_request_method_name
- http_request_method_register
- http_request_method_unregister
- http_request
- http_redirect
- http_send_content_disposition
- http_send_content_type
- http_send_data
- http_send_file
- http_send_last_modified
- http_send_status
- http_send_stream
- http_throttle
- http_build_str
- http_build_url
Коментарии
Here's a PHP implementation for those less interested in installing a pecl module. It's a bit naive (doesn't handle multiple byte encodings), but it works. I'm also including my is_hex() implementation at no additional cost.
<?php
if (!function_exists('http-chunked-decode')) {
/**
* dechunk an http 'transfer-encoding: chunked' message
*
* @param string $chunk the encoded message
* @return string the decoded message. If $chunk wasn't encoded properly it will be returned unmodified.
*/
function http_chunked_decode($chunk) {
$pos = 0;
$len = strlen($chunk);
$dechunk = null;
while(($pos < $len)
&& ($chunkLenHex = substr($chunk,$pos, ($newlineAt = strpos($chunk,"\n",$pos+1))-$pos)))
{
if (! is_hex($chunkLenHex)) {
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
return $chunk;
}
$pos = $newlineAt + 1;
$chunkLen = hexdec(rtrim($chunkLenHex,"\r\n"));
$dechunk .= substr($chunk, $pos, $chunkLen);
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
}
return $dechunk;
}
}
/**
* determine if a string can represent a number in hexadecimal
*
* @param string $hex
* @return boolean true if the string is a hex, otherwise false
*/
function is_hex($hex) {
// regex is for weenies
$hex = strtolower(trim(ltrim($hex,"0")));
if (empty($hex)) { $hex = 0; };
$dec = hexdec($hex);
return ($hex == dechex($dec));
}
?>