dsfdsf21321 2012-01-22 20:15
浏览 12
已采纳

PHP:字符串中的字节数,原始HTTP

I am trying to get the number bytes in a PHP string. I seem to be running into a problem trying to send raw HTTP requests (PROPFIND, REPORT), and getting the proper length of the content. From which point, following the headers, do I start counting the content? And at which point, do I stop?

  • 写回答

2条回答 默认 最新

  • douzi6992 2012-01-22 20:20
    关注

    You count the complete content, starting after the two linebreaks that delimit the header section:

    $contentlength_bytes = strlen(strstr($http, "
    
    ")) - 4;
    

    If you're doing it that way already i guess you might have run into problems with encoding...

    When your content has multibyte characters, using strlen() to return it's bytelength might not work correctly as multibyte characters might get interpreted as one byte under certain system configurations (see edit below - and comments for that part).

    This will give you the correct bytelength of any content-string you feed it under any system configuration:

    $contentlength_bytes = mb_strlen(strstr($http, "
    
    "), 'latin1') - 4;
    

    Edit:
    As Jon pointed out in the comments, this is not always needed as strlen() will return the correct bytecount of a string in most circumstances.

    I just added this method of measuring as on multibyte systems and under certain circumstances (for example mbstring.func_overload set to 2) strlen() is not safe to use against binary strings.

    The above method is the only known (to me) way to completely binary safe calculate the byte-length of a given string. And i tripped over this a couple of times already..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题