How to implement php json_encode without backslashes

藏色散人
Release: 2023-03-14 19:18:01
Original
3324 people have browsed it

php json_encode不要反斜杠的实现方法:1、使用PHP自带的参数“JSON_UNESCAPED_SLASHES”;2、通过“str_replace("\\/", "/", json_encode($url));”替换。

How to implement php json_encode without backslashes

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php json_encode不要反斜杠怎么实现?

解决 json_encode 格式化的数据出现反斜杠的问题

让 json_encode 格式化的数据不出现反斜杠

问题描述:

        在我们给前端或者服务器返回 json 格式数据的时候,用 json_encode 格式转换后,总是出现反斜杠,而默认的 json_encode 是会对 / 转义成 \/ 的,如下返回图片地址的时候:

"data":"http:\/\/www.****.com\/static\/admin\/upload\/1545030861.jpg"
Copy after login

而我们想要的是下面格式:

"data":"http://www.****.com/static/admin/upload/1545023021.jpg"
Copy after login

解决办法:

1. 使用PHP自带的参数:JSON_UNESCAPED_SLASHES

$url = 'http://www.chyblog.com/static/admin/upload/1545023021.jpg';echo json_encode($url,JSON_UNESCAPED_SLASHES);
Copy after login

2. 也可以直接替换

$url = 'http://www.chyblog.com/static/admin/upload/1545023021.jpg';echo str_replace("\\/", "/", json_encode($url));
Copy after login

两者得到的结果是一样的!

推荐学习:《PHP视频教程

The above is the detailed content of How to implement php json_encode without backslashes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!