存储字符串时Php变量的大小限制是多少?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了存储字符串时Php变量的大小限制是多少?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
情况就是这样:
我有一个名为myDB.sql的2Gb转储文件.它是一个转储文件,用于删除现有数据库并使用视图和触发器创建新数据库.所以我有很多代码行的字符串myDB_OLD.
我想将这些字符串出现更改为myDB_NEW.
我可以使用notePad轻松地做到一点.但记事本不会打开2Gb文件.
我所做的是一个 PHP代码,它逐行读取并查找并替换我想要的字符串.

这是代码

$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来找出问题所在,但没有运气.

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

这是新代码

$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",$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变量?

是. A string can be as large as up to 2GB (2147483647 bytes maximum).您无法通过增加PHP.ini中的memory_limit指令来覆盖此限制.

From php7在64位系统中没有这个限制:

脚本宝典总结

以上是脚本宝典为你收集整理的存储字符串时Php变量的大小限制是多少?全部内容,希望文章能够帮你解决存储字符串时Php变量的大小限制是多少?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。