Link to home
Start Free TrialLog in
Avatar of gautam_reddyc
gautam_reddyc

asked on

Having a problem json encoding a string with quotes

Hello I am json_encoding a string to send it as an argument from PHP to a Javascript function. But I get a null value once I json encode it. Is there a way to bypass and get this string to javascript. I tried it without json encoding, but I get an error

The string that I need to json encode to javascript is  " Handsets/Devices -Without CD  - 4.5” W x 6.5” H x 1.75” minimum to 3.75” maximum D (114.3mm W x 165.1mm H x 44.45mm minimum to 95.25mm maximum D) ".
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you using the additional parameter for JSON options?

http://uk2.php.net/json_encode

 json_encode( $a, JSON_HEX_APOS | JSON_HEX_QUOT );

You need to use json_encode when you have special character like double quote. This is your case.
COuld you paste your current code ?
What is your PHP version ? You need a version greater or equal to 5.2.0

http://php.net/manual/fr/function.json-encode.php
>But I get a null value once I json encode it

What is the type of the object you're trying to encode ? COuld you check the object is valid or/And not null ?
Avatar of gautam_reddyc
gautam_reddyc

ASKER

no, but when I try it, it gives me a warning

 json_encode() expects exactly 1 parameter, 2 given in /var/www/CEExtranet/VIFTool/VIFBuildTemplate.php on line 254
@leakim

I am not encoding an object, I am only encoding the string. When I pass a string without double quotes I get errors, I check them using error console in firefox, so to avoid that I json encode it.

Yes I can print the string before encoding and once I json_encode it I get the value as null.
You mean after this line you get null in $myjson ?


$myjson = json_encode(' Handsets/Devices -Without CD  - 4.5” W x 6.5” H x 1.75” minimum to 3.75” maximum D (114.3mm W x 165.1mm H x 44.45mm minimum to 95.25mm maximum D) ');

Open in new window

" json_encode() expects exactly 1 parameter, 2 given"

In that case you are on a PHP version prior to 5.3 which means.... Oops!

Your simplest option is to dump the "smart quotes" in 4.5” W x 6.5” and use regular quotes like so 4.5" W x 6.5" which will sort the problem.
@leakim -- yes
I have used var_dump to get the results.
string(3) "TBD"
--string(5) ""TBD""

string(45) "Handsets/Devices -Without CD  - 4.5” W x 6.5”"
--string(4) "null"

@bport
could you please tell me how do I use smart quotes??
Is it possible that you need to escape the quotes in the text? Try putting the whole thing in single quotes. Can I see the actual code?
well this would be dynamic, and those are not quotes but double primes. Double primes are being used of inches. Sorry for the confusion, if they were quotes, add slashes would have worked.

I am looking for regular expressions to solve the problem.

if (preg_match("/”/", $OEMAnswer, $matches)) {
  echo "Match was found <br />";
  print_r($matches);
}

Would this preg_match give me matches for all the double primes in my string??
Use : utf8_encode

$myjson = json_encode(utf8_encode(' Handsets/Devices -Without CD  - 4.5” W x 6.5” H x 1.75” minimum to 3.75” maximum D (114.3mm W x 165.1mm H x 44.45mm minimum to 95.25mm maximum D) '));
// echo "Match was found <br />";
echo json_encode( utf8_encode( $matches ) );

Open in new window

@leakim971

Thanks it works well for me to get the data to javascript, is there anyway to strip these utf8 encoding on the javascript side??
you should have only one unicode char for this : ”
so use : myStr.replace(/\\u008/g,"”"); //

\u008 is(?) the unicode for ”

If you need more help, please post the javascript string you get with the utf8 enconding chars
yes! the reason I asked you was, I performed a similar replace and I get a string with some other character. The string looks like this "Handsets/Devices -Without CD - 4.5¿ W x 6.5¿".

I tried the replace with \u008 too, but still gives me the same string output.
Used this for rel
answer.replace(/\\u0094/g,"”");

Used this for json encoding as advised.
$answer = json_encode(utf8_encode($answer));

Open in new window

I guess I do not have utf8 on my work laptop, because when I submitted the string to you, they look like some numbers inside a box, but on submit they look like inverted question marks.

How do I get utf8 encoding?? can you help me with that?
try this :

answer.replace(/\\u0094/g, String.fromCharCode(\u0094));

or

answer.replace(/\\u0094/g, new String(\u0094));

or


answer.replace(/\\u0094/g, \u0094);

I know you follow me
I have tried all the three but all I get is this string with a box containing numbers in it
"Handsets/Devices -Without CD - 4.5¿ W x 6.5¿". I have attached an image for you to understand what the exact problem is.

I believe it has something to do with charset.
WierdNumbers.png
confirm you did :


answer = answer.replace(/\\u0094/g, new String("\u0094") );

and not :

answer.replace(/\\u0094/g, new String("\u0094") );

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
answer = answer.replace( regex, String.fromCharCode(8220));

Yay!! it finally worked. Thanks to you Leakim!!

But I just changed the charCode, since I was trying the double prime and not a double quote.

Thanks.
You are a javascript Yodha!!
great! thanks for the points! have a nice week-end!