<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Transcending Frontiers &#187; json_decode() Not Working in PHP?</title>
	<atom:link href="http://blog.thefrontiergroup.com.au/tag/magic-quotes/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thefrontiergroup.com.au</link>
	<description>Your peek inside the collective mind of The Frontier Group</description>
	<lastBuildDate>Mon, 02 Apr 2012 04:32:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>json_decode() Not Working in PHP?</title>
		<link>http://blog.thefrontiergroup.com.au/2008/11/json_decode_php/</link>
		<comments>http://blog.thefrontiergroup.com.au/2008/11/json_decode_php/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 06:54:28 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Magic Quotes]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=147</guid>
		<description><![CDATA[&#8211; Check out some of our more recent Ruby on Rails blog posts. If you&#8217;d like to hire our team, get in touch &#8211; This took me a few minutes today to work out and find an answer on Google so I thought I&#8217;d share it. I had a string that I was posting from a [...]]]></description>
			<content:encoded><![CDATA[<p><em>&#8211; Check out some of our more recent </em><a title="Ruby on Rails posts" href="http://thefrontiergroup.com.au/blog/category/ruby-on-rails/"><em>Ruby on Rails blog posts</em></a><em>. If you&#8217;d like to hire our team, <a title="Get in touch" href="http://thefrontiergroup.com.au/pages/contact-us">get in touch</a></em><em> &#8211;</em></p>
<p>This took me a few minutes today to work out and find an answer on Google so I thought I&#8217;d share it.</p>
<p>I had a string that I was posting from a form and while Firebug on the client said that the string was valid, on the server side PHP&#8217;s json_decode() function was returning me a <a href="http://en.wikipedia.org/wiki/Null_(computer_programming)">NULL</a> indicating invalid <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>. I spit out the string using good old var_dump() and I could see straight away that I would need to strip some slashes from it, but I couldn&#8217;t figure out why.</p>
<p>I don&#8217;t like to simply know how to fix something, I like to know why it was broken in the first place! A bit of Googling gave me the answer, it turned out to be because of the magic_quotes setting in PHP.</p>
<p>Instead of just wrapping everything in a stripslashes() call I decided to write a wrapper function that will take into account whether magic_quotes is on or off. It will also allow me to do whatever I want later one if I wanted to parse a particular type of data or something.</p>
<p>Here is the code. As you can see it&#8217;s nothing fancy, but it works and hopefully it saves people some time.</p>
<pre class="php">function _json_decode($string) {
	if (get_magic_quotes_gpc()) {
		$string = stripslashes($string);
	}

	return json_decode($string);
}</pre>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2008/11/json_decode_php/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2008/11/json_decode_php/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

