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 '';
    while(
$in != '') {
        
$lf_pos strpos($in"\012");
        if(
$lf_pos === false) {
            
$out .= $in;
            break;
        }
        
$chunk_hex trim(substr($in0$lf_pos));
        
$sc_pos strpos($chunk_hex';');
        if(
$sc_pos !== false)
            
$chunk_hex substr($chunk_hex0$sc_pos);
        if(
$chunk_hex == '') {
            
$out .= substr($in0$lf_pos);
            
$in substr($in$lf_pos 1);
            continue;
        }
        
$chunk_len hexdec($chunk_hex);
        if(
$chunk_len) {
            
$out .= substr($in$lf_pos 1$chunk_len);
            
$in substr($in$lf_pos $chunk_len);
        } else {
            
$in '';
        }
    }
    return 
$out;
}

?>