If you find yourself talking to Web servers via raw sockets, you will also find yourself being sent 'Content-transfer-encoding: chunked' results. Then you will get to find out that how you should deal with this is ill-defined and standards compliance spotty. This snippet provides a simple, reasonably robust chunked transfer decoding mechanism that's handled everything that's been thrown at it so far.

PHP Code:

function transfer_encoding_chunked_decode($in) {
$out '';
$in != '') {
$lf_pos strpos($in"\012");
$lf_pos === false) {
$out .= $in;
$chunk_hex trim(substr($in0$lf_pos));
$sc_pos strpos($chunk_hex';');
$sc_pos !== false)
$chunk_hex substr($chunk_hex0$sc_pos);
$chunk_hex == '') {
$out .= substr($in0$lf_pos);
$in substr($in$lf_pos 1);
$chunk_len hexdec($chunk_hex);
$chunk_len) {
$out .= substr($in$lf_pos 1$chunk_len);
$in substr($in$lf_pos $chunk_len);
        } else {
$in '';