存储字符串时,Php 变量的大小限制是多少? [英] What is the limit in size of a Php variable when storing string?

查看:34
本文介绍了存储字符串时,Php 变量的大小限制是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况是这样的:我有一个名为 myDB.sql 的 2Gb 转储文件.它是一个转储文件,用于删除现有数据库并创建一个带有视图和触发器的新数据库.所以我将字符串 myDB_OLD 扩展到许多代码行.我想将这些字符串的出现更改为 myDB_NEW.我可以使用 notePad++ 轻松完成此操作.但是记事本不会打开 2Gb 文件.我所做的是一个 PHP 代码,它逐行读取并找到并替换我想要的字符串.

This is the case: I have a 2Gb dump file called myDB.sql. It is a dump file that deletes a existing database and creates a new one, with views and triggers. So I have the string myDB_OLD spread for many lines of the code. I would like to change these strings occurrences to myDB_NEW. I could do this easelly using notePad++. But notepad does not opens a 2Gb file. What I did is a PHP code that reads line by line and find and replace the string I want.

这是代码:

$myfile2 = fopen("myDB.sql", "r") or die("Unable to open file!");//Reads the file
while (!feof($myfile2)) {//Pass trough each line
    $str=fgets($myfile2);//Store the line inside a variable
    if (preg_match("/myDB_OLD/",$str)) {//If the string I want to change exists - replace it and conacatenate
        $newStr .= str_replace("myDB_OLD","myDB_NEW",$str);
    } 
    else {//If not concatenate it
        $newStr .=$str;
    }
}//End of while
fclose($myfile2);
//Save the  newStr in a new file named 
$myfile = fopen("newDB.sql", "w") or die("Unable to open file!");
fwrite($myfile,  $newStr);
echo "finished";

此代码检索文件的每一行更改字符串,在变量中连接并创建一个新文件.它应该有效,但事实并非如此.我不知道为什么.我正在使用 xdebug 找出问题所在,但没有运气.

This code retrieve each line of the file changes the string, concatenate in variable and creates a new file. It should works but it is not. I dont know why. I am using xdebug to figure out what is the issue, but no luck.

所以我改变了方法.我没有将每一行保存在一个变量中,而是直接将其保存在一个文件中,效果很好.

So I change the approach. Instead of save each line in a variable, I save it directly in a file and that works good.

这是新代码:

$myfile = fopen("newDB.sql", "w") or die("Unable to open file!");//Creates a new file "newDB.sql"

$myfile2 = fopen("myDB.sql", "r") or die("Unable to open file!");//Reads the file
while (!feof($myfile2)) {//Pass trough each line
        $str=fgets($myfile2);//Store the line inside a variable
        if (preg_match("/myDB/",$str)) {//If the string I want to change exists - replace it . (Do not concatenate)
            $strRep=str_replace("myDB","myDB_NEW",$str);
        }
        else {
            $strRep =$str;
        }

        fwrite($myfile,  $strRep);// Add the new line to the file "newDB.sql"
}//End of while
fclose($myfile);
fclose($myfile2);

echo "finished";

好的,我解决了我的问题,但它引发了一个想法.第一个代码的问题是什么?我认为问题是要存储在 PHP 变量 2Gb 中的信息量.那么,用于存储值(在本例中为字符串)的 PHP 变量的大小是否有限制?如果是,我该如何检查或更改它?任何 php.ini 变量?

Ok, I solved my issue but it raises a thought. What is the issue of the first code? I think the issue is the amount of information to be stored in a PHP variable, 2Gb. So, is there a limit in size to a PHP variable to stores value, in this case, a string ? If yes how can I check or change it? Any php.ini variable?

推荐答案

那么,用于存储值(在本例中为字符串)的 Php 变量的大小是否有限制?

So, is there a limit in size to a Php variable to stores value, in this case, a string ?

是的.一个字符串最大可达 2GB(最大 2147483647 字节).您可以通过增加 memory_limit 来覆盖此限制php.ini 中的指令.

Yes. A string can be as large as up to 2GB (2147483647 bytes maximum). You can override this limit by increasing memory_limit directive in php.ini.

从 php7 没有这个限制64 位系统:

From php7 there's not that limitation in a 64 bit system:

在 64 位版本中支持长度 >= 2^31 字节的字符串.

Support for strings with length >= 2^31 bytes in 64 bit builds.

这篇关于存储字符串时,Php 变量的大小限制是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆