在 php 中将“\/”替换为“/”

Replacing "\/" with "/" in php

我正在尝试用 / 替换 php 中字符串输出中出现的所有 \/,但它不起作用..

这是我的代码:

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

但我仍然在网页的输出中得到这样的字符串,例如:

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

或类似的东西:

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

如果我这样调换两个函数的顺序:

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

我把斜线写对了,但是德语字母以一种奇怪的形式出现,比如:“\u00df”或 "u00f6\u00df"...例如世界 "große" 会被写成喜欢 "gro\u00dfe"

有人想解决这个问题吗?获得正确的德语字母和 URI?不喜欢 "https://img.xxxx.com/images/channel-resources/1/def/43/0/1/defintion.png"?

您使用了错误的常量。

使用 JSON_UNESCAPED_SLASHES 而不是 JSON_UNESCAPED_UNICODE 以防止转义 json_encode().

中的斜杠

您可以使用 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE 指定两者。

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

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

尝试回显 $output 并检查它,我几乎可以肯定是你正在使用的 json_encode() 为你添加了 \

\u00* 是 Unicode 字母。

尝试将其解析为 html_entities

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