PHP Serialize()和Unserialize()问题

I've been working on some very large forms lately and I've come to the conclusion that creating a database scheme around them wouldn't be the best option because:

我最近一直在处理一些非常大的表单,但得出的结论是,围绕它们创建数据库方案不是最佳选择,因为:

  1. My customers don't need to analyze all form submissions as a whole -- form information is simply used on a per-submission basis (like a job application, for example).

    我的客户不需要整体分析所有表单提交-表单信息仅在每个提交的基础上使用(例如,像求职申请)。
  2. Making updates to these forms would be very costly since it would take quite a bit of time to add and remove DB fields as well as update the HTML form.

    对这些表单进行更新将非常昂贵,因为添加和删除数据库字段以及更新HTML表单将花费大量时间。
  3. I'd like to revert the information into an array format just like it came in easily.

    我想将信息还原为数组格式,就像它很容易输入一样。

For that reason, I've been using the serialize() and unserialize() functions often. Serializing an array keeps the information in an array format, so to speak, but in one long string. Anyways, I ran into the following error when testing unserialize on some information that I had serialized:

因此,我经常使用serialize()和unserialize()函数。 序列化一个数组可以使信息保持为数组格式,可以说是一个长字符串。 无论如何,在对我已序列化的某些信息进行反序列化测试时,我遇到了以下错误:

Notice: unserialize(): Error at offset 2 of 52 bytes in file.php on line 130

It turns out that if there's a ", ', :, or ; in any of the array values the serialization gets corrupted. I've found the following fix for this issue on PHP.net:

事实证明,如果在任何数组值中都存在“,” 、:或;,则序列化会被破坏。我在PHP.net上找到了针对此问题的以下修复程序:

//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));

//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));

It's a great fix to simple problem!

这是解决简单问题的绝妙方法!

翻译自: https://davidwalsh.name/php-serialize-unserialize-issues

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值