dongtang1966 2014-01-24 12:57
浏览 89

PHP strpos和substr函数导致'允许的内存大小耗尽'错误

The part of my code is the following:

while( $pos1 = stripos( $description, '<style' ) ) {
  $pos2 = stripos( $description, '</style>' ) + 8;
  $description = substr( $description, 0, $pos1 ).
                 substr( $description, $pos2 );     //   <= This string causing the error
}

Sometimes (not all the time!) I receiving the error:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 107663188 bytes) in /path/to/my/script.php on line 88

88'th line indicated by the '<=' arrow above.

The size of $description variable is about 100 kB's. Besides I don't see any reason to believe that this code can cause accumulation of memory allocation without to be released.

Do you see any flaws in my code?

  • 写回答

2条回答 默认 最新

  • doulue1949 2014-01-24 13:08
    关注

    This code causes memory error

    $description = 'hello<style></style>hello<style>';
    while( $pos1 = stripos( $description, '<style' ) ) {
        $pos2 = stripos( $description, '</style>' ) + 8;
        $description = substr( $description, 0, $pos1 ).
            substr( $description, $pos2 );     //   <= This string causing the error
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?