Powered by Blogger.

PHP – disable slashes automatically added when using json_encode


In PHP if you call json_encode it will add slashes to your sting, but if you want to use update_optionn just want URL without slashes. Here is easy to let you remove slashes in JSON

If you have the following codes:

$site_logo = array();
$site_logo['src'] = 'https://www.waytowp.com/wp-content/uploads/2014/09/logo-white-Shadow-4.png';

echo json_encode( $site_logo );

Then you will get:

"http:\/\/www.waytowp.com\/wp-content\/uploads\/2014\/09\/logo-white-Shadow-4.png"


But if you call json_encode with parameter JSON_UNESCAPED_SLASHES then the slashes won’t be added


$site_logo = array();
$site_logo['src'] = 'https://www.waytowp.com/wp-content/uploads/2014/09/logo-white-Shadow-4.png';

echo json_encode( $site_logo, JSON_UNESCAPED_SLASHES );


You will get:

"src":"https://www.waytowp.com/wp-content/uploads/2014/09/logo-white-Shadow-4.png"}



No comments