Apostrophe/single quotes php and Json

Hello,

I am using Google API to get a word definition.  I am using PHP and the code is below where $file is the json returned from Google.

The echo $json; line has everything (and I am able to parse and retrieve everything I need) but there are no apostrophes.  Any idea how to preserve them???

Many thanks in advance!

Cheers,


$file = substr($file, 2, -10);
$json = preg_replace("/\\\x[0-9a-f]{2}/", "", $file);
json_encode($json);
echo $json;

Open in new window

1CougarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Please provide some sample data.  Also can you provide the javascript where you receive this ajax call.

I've run your code and there's no issue:

<?php
$mystring = "this isn't a test";
$json = preg_replace("/\\\x[0-9a-f]{2}/", "", $mystring);
echo json_encode($json);
?>

Open in new window


gives me: "this isn't a test"
0
1CougarAuthor Commented:
Good that the code works, but the json from Google never has any apostrophes.  For example, for the word "manner", after "social behavior and habits" there is: Example: "Tim apologized for his sons bad manners."  This should be: "Example: Tim apologized for his son's bad manners."  This is not a typo as the json systematically has all apostrophes removed.

This is where I get the json:

$file = file_get_contents('http://www.google.com/dictionary/json?callback=a&q=manner&sl=en&tl=en&restrict=pr,de&client=te');

Open in new window


Thanks again,
0
1CougarAuthor Commented:
For example, if you substitute "flab" for "manner" in above url you have a missing apostrophe...it reads "...a persons..." when it should be "....a person's...."
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

1CougarAuthor Commented:
So, I suspect that the apostrophes are getting stripped out somewhere since I can't believe Google would provide grammatically incorrect text throughout.

??
0
RobOwner (Aidellio)Commented:
I can see them in the raw data that i get when just navigating the browser to that URL:

a({"query":"flab","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"flab","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"phonetic","text":"/flab/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/flab.mp3","language":"und"}],"entries":[{"type":"meaning","terms":[{"type":"text","text":"Soft loose flesh on a person\x27s body; fat","language":"en"}]}]}],"webDefinitions":[{"type":"headword","terms":[{"type":"text","text":"flab","language":"en"}],"entries":[{"type":"meaning","terms":[{"type":"text","text":"loose or flaccid body fat","language":"en"},{"type":"url","text":"\x3ca href\x3d\x22http://wordnetweb.princeton.edu/perl/webwn?s\x3dflab\x22\x3ehttp://wordnetweb.princeton.edu/perl/webwn?s\x3dflab\x3c/a\x3e","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"A FLAB is an abbreviation using four letters.http://acronyms.thefreedictionary.com/FLAB It is an abbreviation of Four Letter ABbreviation. It extends the self-referential humour of the TLA (Three Letter Acronym). It is sometimes referred to as an ETLA (Extended Three Letter Acronym)","language":"en"},{"type":"url","text":"\x3ca href\x3d\x22http://en.wikipedia.org/wiki/Flab\x22\x3ehttp://en.wikipedia.org/wiki/Flab\x3c/a\x3e","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":" Soft, loose flesh on a person\x26#39;s body; fat","language":"en"},{"type":"url","text":"\x3ca href\x3d\x22http://en.wiktionary.org/wiki/flab\x22\x3ehttp://en.wiktionary.org/wiki/flab\x3c/a\x3e","language":"en"}]}]}]},200,null)

Open in new window


But you're removing the apostrophes with this code

$json = preg_replace("/\\\x[0-9a-f]{2}/", "", $mystring);

Open in new window


The \x26 etc that you see throughout the returned JSON is ASCII codes for special characters such as the apostrophe.
0
1CougarAuthor Commented:
So, what am I doing wrong then....do you know why they might be getting removed when passed through my code?

Thanks again....
0
1CougarAuthor Commented:
sorry didn't see the rest of your response.  If I remove that line of code then the json is incorrect....
0
RobOwner (Aidellio)Commented:
It's not that the JSON is incorrect but it has encoded the non alphabetical characters using ASCII codes (HEX equivalent).  See here for a conversion table:  http://www.ascii.cl/

So what you're going to need to do is update that so it's visible to the browser, ie replace \x27 with \u0027, which fortunately you can do with REGEX. See the grouped items using the ( ) and again referencing them using the first match $1

$json = preg_replace("/\\\x([0-9a-f]{2})/", "\\u00$1", $file);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
1CougarAuthor Commented:
It works!  Thank you!

Cheers!
0
RobOwner (Aidellio)Commented:
No problem, you were almost there anyway ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.