0

I don't know why but it seems like wordpress is adding a second backslash when I'm using the following functions:

addslashes($str_with_single_quotes)
addslashes(stripslashes($str_with_single_quotes));
esc_sql($str_with_single_quotes)
str_replace("'", "/'", $str_with_single_quotes)

What I'm doing is that I'm fetching data from different API's and then converting those data to a json string so I can access them later on:

$item_data = array(
    'item_title' => __(addslashes(stripslashes($item_name))),
    'product_review' => __(addslashes(stripslashes($product_review))),
    'freebase_text' => __(addslashes(stripslashes($freebase_result['text'])))
);

$data = json_encode($item_data);

It doesn't seem like the localization function (__()) is the one that's causing the problem since the problem is still there even I completely remove it.

I also tried preg_replace_all but I got a fatal error that the function wasn't recognize. I also tried using str_replace, esc_sql, and str_replace after the array was converted a json string but no luck.

I've run out of ideas on how to solve this problem. Simply using json_encode without processing the strings doesn't actually do anything and the single quotes are left unescaped.

Magic quotes are also disabled:

magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off

Thanks in advance.

3

In your code json_encode() function causes to add second backslash on the following line.

$data = json_encode($item_data);

Add the following code in place of above code so it will replace double backslashes with single backslash in data returned by json_encode function.

$data = str_replace("\\\'","\\'",json_encode($item_data ));

Visit following links for more information on json_encode function.

json_encode function

json_encode function Predefined Constants

Tell me whether it resolved your problem or i will find another solution for it.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.