How to serialize strings with apostrophes using PHP

By Matt Dunlap on May 17th, 2010 2 comments
Clip to Evernote Tags:

If not handled correctly, apostrophes will cause all kinds of problems when coding. Recently, I ran across a problem when  trying to serialize an array of strings that could possibly have apostrophes. The solution is to encode to base64, then decode when reading the data.

One thing to note is that when you serialize the data as base64 you will not be able to read the data from the database due to the encoding. Of course this really isn’t a reason not to do this, and serialized data wasn’t that easy to read anyway.

Serialize

$data = "Hey, I have a special character's";
$insert_data = base64_encode(serialize($data));

Unserialize

$data = unserialize(base64_decode($insert_data));

That’s it. Took me a while to figure it out because I thought a simple “add_slashes” was going to take care of it… but it didn’t work.


My next post is going to be awesome... Subscribe to my feed so you don't miss it

Large RSS Feed Button


Previous Post Next Post

2 Responses to How to serialize strings with apostrophes using PHP

zi
Comment added: 22 July, 2010 at 3:38 pm

Thanks Matt! I’ve been banging my head against this problem for the last 2-3 hours trying different combinations of mysql_real_escape_string, addslashes, stripslashes. You are a lifesaver!

Matt Dunlap
Comment added: 22 July, 2010 at 3:48 pm

Yep, I did the exact same thing!

Post a Comment

Name:   
Email:   
Website:   
Comments:   
alert icon Do you need help with your website? I can offer full system admin, website development and Internet marketing services
Domain Name Deals
.COM for $1.99
With any non domain name purchase
Free Private Registration - When you buy or transfer 5 domains at opendls.com
Domain Name Reseller Accounts
Low buy rates - Very Profitable
Matt Dunlap on Why am I getting redirect loop errors with FireFox 3.6.8?
Hi, thanks for the heads up... Have you experienced this problem with any other websites? It's reall[...]
Stacy on Why am I getting redirect loop errors with FireFox 3.6.8?
I was attempting to follow your links to these tutorials from your youtube video, and I was using th[...]
Matt Dunlap on 301 redirects will lower your Google Pagerank
You want 301 redirect on same site redirects mainly to eliminate duplicate content. For example, if [...]
Kate Mag on 301 redirects will lower your Google Pagerank
great info. Does this mean that only affecting 301 redirect to another domain. I have 301 redirect t[...]
Mike Johnson - Real Estate Agent on Why is making extra money a bad habit?
Hey man, this is some great stuff. The only thing I can't figure out is why this sort of thing isn'[...]

Top Guest Bloggers

LoneWolf
View Posts | RSS

Guest Bloggers Welcome!


Powered By: Free Premium WordPress Theme | Copyright 2007 - 2010 mattdunlap.org Sitemap