如何在访问 json 数据时删除到达文件路径字段的正斜杠

How to remove forward slash which gets to the filepath field while acessing json data

Screenshot of path stored in the database

Json data which I retrieved

PHP 我用来编码的代码 JSON:

<?php
$host='localhost'; $username=''; $pwd=''; $db="";

$con=mysqli_connect($host,$username,$pwd,$db) or die('Unable to connect');

if(mysqli_connect_error($con))
{
    echo "Failed to Connect to Database ".mysqli_connect_error(); 
}

$sql="SELECT * FROM qtb";

$result=mysqli_query($con,$sql); 

if($result) 
{
    while($row=mysqli_fetch_array($result))     
    {
        $data[]=$row;   
    }       

    print(json_encode(array('QOB' => $data))); 
}

mysqli_close($con);
?>

这假设您在问题中实际上是指反斜杠 - 请参阅评论。

尝试使用 json_encode 中的 JSON_UNESCAPED_SLASHES 选项:

json_encode(array('QOB' => $data), JSON_UNESCAPED_SLASHES)

反斜杠 ('\') 正在转义正斜杠 ('/') - 因此,如果您真的希望删除正斜杠(如标题所述),那么您的路径将不正确。如果你真的是说 'replace the forward slash with backward slash' 那么那就不一样了。

将正斜杠改为反斜杠

print(str_replace('/', '\', json_encode(array('QOB' => $data), JSON_UNESCAPED_SLASHES)));

沙盒: http://sandbox.onlinephpfunctions.com/code/babf45f149226518a796714323dbff6040664a9b