gpt4 book ai didi

php - 通过 php 代码从 mysql 导出时出现错误的斜杠

转载 作者:行者123 更新时间:2023-11-29 02:21:48 24 4
gpt4 key购买 nike

我使用这段代码从表中导出数据,但是如果它在数据库中有 url,它会给出错误的格式

 ex {"site_url":"tiger","site_name":"hassan:\/\/tiger-sat.net\/nn.mp4"}

应该是hassan://tiger-sat.net/nn.mp4任何知道如何修复请,,,

<?php   
//PDO is a extension which defines a lightweight, consistent interface for accessing databases in PHP.
$db=new PDO('mysql:dbname=db;host=localhost;','root','pass');
//here prepare the query for analyzing, prepared statements use less resources and thus run faster
$row=$db->prepare('select * from channel');

$row->execute();
$json_data=array();
foreach($row as $rec)
{

$json_array ['site_url']=$rec ['site_url'];
$json_array['site_name']=$rec['site_name'];

//here pushing the values in to an array
array_push($json_data,$json_array);

}
//built in PHP function to encode the data in to JSON format
echo json_encode($json_data);
?>

最佳答案

没有错。 json_encode 函数只是转义数据,它是 100% 有效的,不应该产生任何差异,转义数据更好。但如果您不想转义数据并拥有 php > 5.4 或更高版本,您可以使用

echo json_encode($json_data, JSON_UNESCAPED_SLASHES); 

相反。

但是如果你有一个 php < 5.4 你可以改用下面的代码:

$encoded =  json_encode($json_data);
$unescaped = preg_replace_callback('/\\\\u(\w{4})/', function ($matches) {
return html_entity_decode('&#x' . $matches[1] . ';', ENT_COMPAT, 'UTF-8');
}, $encoded);
echo $unescaped;

另一种选择是使用:

 echo str_replace('\\/', '/', json_encode($json_data));

关于php - 通过 php 代码从 mysql 导出时出现错误的斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30470374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com