开发者

How to serialize URL and make it still readable in PHP?

I need to serialize array which contains URLs:

Array( 'url1' => 'http://www.example.com', 'url2' => 'http://www.example1.com' ) and store it in DB.

When I serialize开发者_如何学JAVA it standard way, it doesn't work as it contains special chars. I found solution to encode it with base64_encode . Then it works but string is unreadable from me in DB manager program. Is there a way to make this work without base64_decode ?


  1. It should always set off a red flag when you're trying to store serialized data in a relational database. Normalize your schema so you don't have to serialize.
  2. Storing your data in a poor format so it is readable while in the DB is not a good idea. You want to store it in a format that is the most efficient for database system, then update your manager to unserialize it when you are ready yo display.
  3. json_encode is popular these days, and helps make your data portable.


If you're using PHP 5+, try using JSON instead of the native PHP serializer. JSON is a lot more portable.

But your problem could be with automatic escaping of quotes. It would be helpful if you can show examples of your input & output to/from the DB.


There's no reason why serialize shouldn't work on this example, so it may be more to do with adequate escaping of inputs in your SQL query rather than an issue with the kind of serialisation you're doing. If you're using MySQL, try running the serialised data through mysql_real_escape_string() before you concatenate it into your SQL statement.

Separately, I tend to prefer json_encode() for serialisation of values to a DB field, because serialise tends to make serialised data that is very hard to read manually, and extremely difficult to edit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜