ZelelB
ZelelB

Reputation: 2000

Replacing "\/" with "/" in php

I am trying to replace all occurences of \/ in a string output in php with /, but it is not working..

Here is my code:

$output = str_replace("\\/", "/", $output);
echo json_encode($output, JSON_UNESCAPED_UNICODE );
echo json_encode($output, JSON_UNESCAPED_SLASHES);

but I am still getting such strings in the output on the webpage, like:

https:\/\/img.xxxx.com\/images\/channel-resources\/1\/def\/43\/0\/1\/defintion.png

or something like that:

https:\/\/img.yyyy.de\/images\/channel-resources\/1\/obchi\/43\/0\/1\/obchi_1.png

If I switch the order of the two functions like that:

$output = str_replace("\\/", "/", $output);
echo json_encode($output, JSON_UNESCAPED_SLASHES);
echo json_encode($output, JSON_UNESCAPED_UNICODE );

I get the slashes written right, but the germans letters are appearing in a weird form, like: "\u00df" or "u00f6\u00df"... for example the world "große" would be written like "gro\u00dfe"

Anyone an idea to fix that? to get the german letters and the URIs written right? not like "https://img.xxxx.com/images/channel-resources/1/def/43/0/1/defintion.png"?

Upvotes: 0

Views: 5482

Answers (3)

Martijn
Martijn

Reputation: 3754

You're using the wrong constant.

Use JSON_UNESCAPED_SLASHES instead of JSON_UNESCAPED_UNICODE to prevent escaping the slashes in json_encode().

You can specify both using JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE.

See http://php.net/manual/en/json.constants.php

$output = str_replace("\\/", "/", $output);
echo json_encode($output, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

Upvotes: 4

The Badak
The Badak

Reputation: 2030

\u00* are Unicode letters.

Try this to parse into html_entities

$output = 'http:\/\/ßßüüää.com\/';
$output = str_replace("\\/", "/", $output);
$output = htmlentities($output, ENT_COMPAT, "UTF-8");
echo json_encode($output, JSON_UNESCAPED_SLASHES);

Upvotes: 0

rypskar
rypskar

Reputation: 2092

Try to echo $output and check it, I am almost sure it is the json_encode() you are using that adds the \ for you

Upvotes: 0

Related Questions