簡體   English   中英

PHP strpos和substr函數導致“允許的內存大小用盡”錯誤

[英]PHP strpos and substr functions causes 'Allowed memory size exhausted' error

我的代碼部分如下:

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

有時(並非總是如此!),我收到錯誤消息:

致命錯誤:在第88行的/path/to/my/script.php中,耗盡了268435456字節的允許的內存大小(試圖分配107663188字節)

第88行由上方的“ <=”箭頭指示。

$description變量的大小約為100 kB。 此外,我看不出有任何理由相信此代碼會導致內存分配的累積而不會被釋放。

您在我的代碼中看到任何缺陷嗎?

此代碼導致內存錯誤

$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
}

看起來好像是由於找不到</stile>導致的問題- $description變量將增長到無窮大。 如果<script>標記以<script ... />形式使用,則可能會出現此代碼,如下所示:

while( $pos1 = stripos( $description, '<style' ) ) {
  $pos2 = stripos( $description, '</style' ) + 8;
  if( $pos2 < $pos1 ) $pos2 = strpos( $description, '/>' ) + 2;
  if( $pos2 < $pos1 ) break 2;
  $description = substr( $description, 0, $pos1 ).
                 substr( $description, $pos2 );
}

看起來不再有錯誤...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM