Reputation: 31
i use this code to export data from table ,but if its have url in db its give wrong format
ex {"site_url":"tiger","site_name":"hassan:\/\/tiger-sat.net\/nn.mp4"}
should be hassan://tiger-sat.net/nn.mp4
any know how to fix please ,,,
<?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);
?>
Upvotes: 2
Views: 90
Reputation: 552
It's not wrong . json_encode function is just escape the data and it's 100% valid and should not create any difference and it's even better to have escaped data. but in case you don't want to escape the data and have php > 5.4 or more you can use
echo json_encode($json_data, JSON_UNESCAPED_SLASHES);
instead.
But in case you have a php < 5.4 you can use the following code instead :
$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;
Another alternative is to use :
echo str_replace('\\/', '/', json_encode($json_data));
Upvotes: 2
Reputation: 3777
I would recommend to update your stack and get on a newer PHP version.
If your just needing to remove the extra slashes from the URL itself, then try stripslashes()
$example = 'hassan:\/\/tiger-sat.net\/nn.mp4';
echo stripslashes($example);
Result:
hassan://tiger-sat.net/nn.mp4
Another Option
$example = 'hassan:\/\/tiger-sat.net\/nn.mp4';
echo str_replace ( '\\' , '' , $example );
Result:
hassan://tiger-sat.net/nn.mp4
However, stripslashes does this exact same thing and would be the better option, it is what the function was made for, stripping backslashes..
If your needing to remove slashes throughout the whole JSON, then I would update PHP and use JSON_UNESCAPED_SLASHES
.
You need to be careful if you aren't going to escape the slashes, they are there for a reason. If you are taking data from the JSON and using it in an SQL query, you could open yourself up to an SQL injection. So you would (and should) be using sanitized queries by using MySQLi or PDO, and prepared statements.
Depending on the use and need, I would not use JSON_UNESCAPED_SLASHES
and strip the slashes when/where needed.
Last Note: preg_replace, and other forms of regular expressions, should be a last resort... Usually, they are for things way more trivial than simply removing a \
from strings. If you just need to remove a character (ie: ) then there are better ways than regular expressions.
Upvotes: 0