I have the following in my categories table. There are Russian symbols present:
id name
1 Обувь
Обувь = Obuv
I'm looking to get "Obuv" as Russian symbols within the output.
echo Category::select('name')->first();
This script should give me "Обувь" as the output, but I'm getting
{"name":"\u041e\u0431\u0443\u0432\u044c"}
What's wrong? How can I get the correct output? If I write "Obuv" in the database, the english "Obuv" will give me the correct output in Laravel. In phpMyAdmin, it gives me Russian symbols without trouble. The problem lies in Laravel.
You are outputting the data by echoing your Category model which outputs in JSON by default in Laravel. This automatically escapes multibyte Unicode characters upon output by default. When you run json_encode(), you could supply the JSON_UNESCAPED_UNICODE option like so:
$data = Category::select('name')->first();
echo json_encode($data, JSON_UNESCAPED_UNICODE);
I should be clear, though. Your Category does not have the escaped data stored within it, the escaping only takes place when you output to JSON. If all you were doing is ensuring you got the correct data back from the database by echoing it, you should be in the clear.
If you're looking for just the raw text of the category's name property, you should be able to output this like so:
echo $data->name;
For more information, see http://php.net/manual/en/function.json-encode.php and https://laracasts.com/discuss/channels/laravel/how-to-prevent-laravel-from-returning-escaped-json-data.
Related
I am working with php and i am trying to get data from "Editor" ( like Tinymyc),But
right now i am getting special characters like "<strong> ,<p>" etc..i just want to get simple data
without any special characters,how can i do this ?I tried with following code
$data=$_POST['editor'];
Give this a try - if your code is correct this should work.
$data = strip_tags($_POST['editor']);
Hello and welcome my Dear friends, I am not posting very often but I am dying from the pain of current problem I have encountered.
I am attaching pictures, but in short array that is created for JSON output in my recruitment page is giving coded outcome which looks like '\u3010\u5510\u6d25\u99c5\u3011\u226a\u5b8c\u5168' in all JSON elements.
I have tried to make mb_encode_check and it returns UTF-8 everywhere. In database coding is utf8_general_ci so it is not a problem.
I also have tried to encode it many times with utf8_decode/encode, json_endcode/decode and others without a success.
Please share with me your wisdom.
Have you tried the JSON_UNESCAPED_UNICODE flag?
<?= json_encode($page_data->json, JSON_UNESCAPED_UNICODE) ?>
If you don't want to escape slashes either, that would be the JSON_UNESCAPED_SLASHES flag:
<?= json_encode($page_data->json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>
i have a MySQL field value with a json object containing Hebrew characters like this:
[{"name":"אספנות ואומנות","value":1,"target":null},{"name":"אופניים","value":2,"target":null}]
(the one in the name field)
This field output is giving me some trouble with a certain web interface.
so, looking around in the database i found another field containing json object and its output works fine.
[{"name":"\u05d0\u05e1\u05e4\u05e0\u05d5\u05ea \u05d5\u05d0\u05d5\u05de\u05e0\u05d5\u05ea","value":1,"target":null},{"name":"\u05d0\u05d5\u05e4\u05e0\u05d9\u05d9\u05dd","value":2,"target":null}]
So i would like to convert the first field to this encoding to see if its solves the output issue.
what is this encoding ? is it UTF-8 ? how can i convert it using PHP ?
i tried to isolate the value and convert it to UTF-8 using
echo iconv("Windows-1255","UTF-8",'אספנות ואומנות');
but its just returning an empty value.
Any help would be great
So, in PHP
json_encode('אספנות ואומנות');
did the trick
I'm writing PHP code that uses a database. To do so, I use an array as a hash-map.
Every time content is added or removed from my DB, I save it to file.
I'm forced by my DB structure to use this method and can't use mysql or any other standard DB (School project, so structure stays as is).
I built two functions:
function saveDB($db){
$json_db = json_encode($db);
file_put_contents("wordsDB.json", $json_db);
} // saveDB
function loadDB(){
$json_db = file_get_contents("wordsDB.json");
return json_decode($json_db, true);
} // loadDB
When echo-ing the string I get after the encoding or after loading from file, I get a valid json (Tested it on a json viewer) Whenever I try to decode the string using json_decode(), I get null (Tested it with var_dump()).
The json string itself is very long (~200,000 characters, and that's just for testing).
I tried the following:
Replacing single/double-quotes with double/single-quotes (Without any backslashes, with one backslash and three backslashes. And any combination I could think of with a different number of backslashes in the original and replaced string), both manually and using str_replace().
Adding quotes before and after the json string.
Changing the page's encoding.
Decoding without saving to file (Right after encoding).
Checked for slashes and backslashes. None to be found.
Tried addslashes().
Tried using various "Escape String" variants.
json_last_error() doesn't work. I get no error number (Get null, not 0).
It's not my server, so I'm not sure what PHP version is used, and I can't upgrade/downgrade/install anything.
I believe the size has something to do with it, because small strings seem to work fine.
Thanks Everybody :)
In your JSON file change null to "null" and it will solve the problem.
Check if your file is UTF8 encoded. json_decode works with UTF8 encoded data only.
EDIT:
After I saw uploaded JSON data, I did some digging and found that there are 'null' key. Search for:
"exceeding":{"S01E01.html":{"2217":1}},null:{"S01E01.html":
Change that null to be valid property name and json_decode will do the job.
I had a similar problem last week. my json was valid according to jsonlint.com.
My json string contained a # and a & and those two made json_decode fail and return null.
by using var_dump(json_decode($myvar)) which stops right where it fails I managed to figure out where the problem was coming from.
I suggest var_dumping and using find dunction to look for these king of characters.
Just on the off chance.. and more for anyone hitting this thread rather than the OP's issue...I missed the following, someone had htmlentities($json) way above me in the call stack. Just ensure you haven't been bitten by the same and check the html source.
Kickself #124
I'm having a problem with some characters like 'í' or 'ñ' working in a web project with PHP and MySQL.
The database table is in UTF-8 charset and the web page is ISO-8859-1 (latin-1). at first look everything is handled ok, but a problem is coming when I use the JSON_ENCODE function of PHP.
When I get a query result, let's say, this row:
| ID | VALUE |
--------------------
| 1 | Línea |
I got the following (correct) array in PHP:
Array("ID"=>"1","VALUE"=>"Línea");
So far, so good. But, when i apply the JSON_ENCODE
$result = json_encode($result);
//$result is {"id":"1","value":"L"}
Then i tried some coding/decoding but i couldn't get the right result.
First I tried to decode the UTF-8 chars like follow:
$result['value'] = utf8_decode($result['value']);
//and I get $result['value'] is "L?a"
Then I tried with mb functions:
$result['value'] = mb_convert_encoding($result['value'],"ISO-8859-1","UTF-8");
//and I get that $result['value'] is "Lnea"
I don't really know why is the Json_encode breaking my string and i can't figure out what else to try. I will appreciate any help :)
Thanks!
The documentation for json_encode states that the function will only work on UTF-8 data. If it's not working for you, it means that your data is not UTF-8.
To understand what's going wrong, you need to know what your connection character set is. Is it UTF-8? Something else? Use SET NAMES utf-8 and see if it makes any difference.
Assuming the connection character set is indeed UTF-8, json_encode should work just fine. Then, you still have the final issue of converting the encoded data to ISO-8859-1. For example:
// assume any strings in $result are UTF-8 encoded
$json = json_encode($result);
$output = mb_convert_encoding($json, 'ISO-8859-1', 'UTF-8');
echo $output;
If it still doesn't work, it means that your UTF-8 strings contain characters not available in the ISO-8859-1 character set. There's nothing you can do about that.
Update:
When debugging complex character set conversions like this, you can use file_put_contents to write intermediate results to a file which you can inspect with a hex editor. This will help confirm that the output of a particular step of the process is correct or not.