PHP Encoding and Decode - php

I have a slice of php encoded code but I don't know how it encoded i mean by which why they encoded this code when i use unphp.net successfully it decode the code and get the real code
simple of encoded code define("\x53\x54\117\103\113\x5f\103\x48\105\x43\113", false) my question is how i can encode another code to be like this and it by which why is encoded please anyone have a knowledge about tell me or know any tool to suggest

I would need more information for me to be able to discern what obfuscator was actually used to encode this, but for this specific string, it appears to be a mix of hex- and octal- encoded letters
"\x53\x54\117\103\113\x5f\103\x48\105\x43\113"
\xFF - Hex
\000 - Octal
The sequence above is encoded as follows (where x is hex and 8 is octal): xx888x8x8x8
This sequence may be random or intentional. Assuming it's random, the following function may be used to encode any string in like manner using random_int() for selecting encoding method at random.
<?php
function hex_octal_rand_encode($s) {
$result = '';
foreach (str_split($s) as $c) {
$d = ord($c);
// select encoding method at random (0 = hex, 1 = oct)
$result .= (random_int(0, 1) === 0)
? '\\x' . dechex($d)
: '\\' . decoct($d);
}
return $result;
}
// Test
$input = "STOCK_CHECK";
$encoded = hex_octal_rand_encode($input);
echo sprintf("Encoded: %s\n", $encoded);
$cmd = sprintf('return "%s";', $encoded);
echo sprintf("Decoded: %s\n", eval($cmd));
?>
Sample outputs (notice the encoded value changes):
Encoded: \x53\x54\117\103\x4b\137\x43\x48\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \x53\124\x4f\x43\113\x5f\x43\110\x45\x43\x4b
Decoded: STOCK_CHECK
Encoded: \123\124\x4f\x43\113\137\x43\x48\105\x43\113
Decoded: STOCK_CHECK

Related

PHP Convert Unicode to text

I am receiving from a form the following urlencoded string %F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E
If I decode it I get the following formatted text: ๐ด๐‘™๐‘’๐‘—๐‘Ž๐‘›๐‘‘๐‘Ÿ๐‘Ž
Is there any way with PHP to get the plain "Alejandra" text from the encoded or decoded string?
I have tried without success several ways to do it with
mb_convert_encoding($string, "UTF-16",mb_detect_encoding($string))
iconv('utf-16', 'utf-8', rawurldecode($string)
and any other solution I could in stackoverflow.
Edit:
I tried the proposed solution $strAscii = iconv('UTF-8','ASCII//TRANSLIT',$str); but it deletes the special characters such as รกรฉรญรณรบรฑรง which we need to stay.
Expected result
input: ๐ด๐‘™๐‘’๐‘—๐‘Ž๐‘›๐‘‘๐‘Ÿ๐‘Ž
output: Alejandra
input: รlejandra
output: รlejandra
Thank you in advance.
urldecode or rawurldecode is sufficient.
$string = "%F0%9D%90%B4%F0%9D%91%99%F0%9D%91%92%F0%9D%91%97%F0%9D%91%8E%F0%9D%91%9B%F0%9D%91%91%F0%9D%91%9F%F0%9D%91%8E";
$str = urldecode($string);
var_dump($str);
//string(36) "๐ด๐‘™๐‘’๐‘—๐‘Ž๐‘›๐‘‘๐‘Ÿ๐‘Ž"
Demo: https://3v4l.org/OMQ35
A special debugger gives me: string(36) UTF-8mb4. This means that there are also UTF-8 characters in the string that require 4 bytes. The character A is the Unicode character โ€œ๐ดโ€ (U+1D434).
Note:
If the special UTF-8 characters cause problems, you can try to display the strings as ASCII characters with iconv.
$strAscii = iconv('UTF-8','ASCII//TRANSLIT',$str);
//string(9) "Alejandra"
What you are getting is called a "psuedo-alphabet", you can see a list of them here: https://qaz.wtf/u/convert.cgi. The one that you appear to be getting can be seen here: https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols
Basically what you need to do is take the string, split it and use a lookup table to convert it back to regular characters. This implementation is terribly efficient but that's because I grabbed the alphabets from the above Wikipedia page and was too lazy to reorganise it.
function math_symbols_to_plain_text($input, $alphabet)
{
$alphabets = [
['a','๐š','๐‘Ž','๐’‚','๐–บ','๐—ฎ','๐˜ข','๐™–','๐’ถ','๐“ช','๐”ž','๐–†','๐šŠ','๐•’'],
['b','๐›','๐‘','๐’ƒ','๐–ป','๐—ฏ','๐˜ฃ','๐™—','๐’ท','๐“ซ','๐”Ÿ','๐–‡','๐š‹','๐•“'],
['c','๐œ','๐‘','๐’„','๐–ผ','๐—ฐ','๐˜ค','๐™˜','๐’ธ','๐“ฌ','๐” ','๐–ˆ','๐šŒ','๐•”'],
['d','๐','๐‘‘','๐’…','๐–ฝ','๐—ฑ','๐˜ฅ','๐™™','๐’น','๐“ญ','๐”ก','๐–‰','๐š','๐••'],
['e','๐ž','๐‘’','๐’†','๐–พ','๐—ฒ','๐˜ฆ','๐™š','โ„ฏ','๐“ฎ','๐”ข','๐–Š','๐šŽ','๐•–'],
['f','๐Ÿ','๐‘“','๐’‡','๐–ฟ','๐—ณ','๐˜ง','๐™›','๐’ป','๐“ฏ','๐”ฃ','๐–‹','๐š','๐•—'],
['g','๐ ','๐‘”','๐’ˆ','๐—€','๐—ด','๐˜จ','๐™œ','โ„Š','๐“ฐ','๐”ค','๐–Œ','๐š','๐•˜'],
['h','๐ก','โ„Ž','๐’‰','๐—','๐—ต','๐˜ฉ','๐™','๐’ฝ','๐“ฑ','๐”ฅ','๐–','๐š‘','๐•™'],
['i','๐ข','๐‘–','๐’Š','๐—‚','๐—ถ','๐˜ช','๐™ž','๐’พ','๐“ฒ','๐”ฆ','๐–Ž','๐š’','๐•š'],
['j','๐ฃ','๐‘—','๐’‹','๐—ƒ','๐—ท','๐˜ซ','๐™Ÿ','๐’ฟ','๐“ณ','๐”ง','๐–','๐š“','๐•›'],
['k','๐ค','๐‘˜','๐’Œ','๐—„','๐—ธ','๐˜ฌ','๐™ ','๐“€','๐“ด','๐”จ','๐–','๐š”','๐•œ'],
['l','๐ฅ','๐‘™','๐’','๐—…','๐—น','๐˜ญ','๐™ก','๐“','๐“ต','๐”ฉ','๐–‘','๐š•','๐•'],
['m','๐ฆ','๐‘š','๐’Ž','๐—†','๐—บ','๐˜ฎ','๐™ข','๐“‚','๐“ถ','๐”ช','๐–’','๐š–','๐•ž'],
['n','๐ง','๐‘›','๐’','๐—‡','๐—ป','๐˜ฏ','๐™ฃ','๐“ƒ','๐“ท','๐”ซ','๐–“','๐š—','๐•Ÿ'],
['o','๐จ','๐‘œ','๐’','๐—ˆ','๐—ผ','๐˜ฐ','๐™ค','โ„ด','๐“ธ','๐”ฌ','๐–”','๐š˜','๐• '],
['p','๐ฉ','๐‘','๐’‘','๐—‰','๐—ฝ','๐˜ฑ','๐™ฅ','๐“…','๐“น','๐”ญ','๐–•','๐š™','๐•ก'],
['q','๐ช','๐‘ž','๐’’','๐—Š','๐—พ','๐˜ฒ','๐™ฆ','๐“†','๐“บ','๐”ฎ','๐––','๐šš','๐•ข'],
['r','๐ซ','๐‘Ÿ','๐’“','๐—‹','๐—ฟ','๐˜ณ','๐™ง','๐“‡','๐“ป','๐”ฏ','๐–—','๐š›','๐•ฃ'],
['s','๐ฌ','๐‘ ','๐’”','๐—Œ','๐˜€','๐˜ด','๐™จ','๐“ˆ','๐“ผ','๐”ฐ','๐–˜','๐šœ','๐•ค'],
['t','๐ญ','๐‘ก','๐’•','๐—','๐˜','๐˜ต','๐™ฉ','๐“‰','๐“ฝ','๐”ฑ','๐–™','๐š','๐•ฅ'],
['u','๐ฎ','๐‘ข','๐’–','๐—Ž','๐˜‚','๐˜ถ','๐™ช','๐“Š','๐“พ','๐”ฒ','๐–š','๐šž','๐•ฆ'],
['v','๐ฏ','๐‘ฃ','๐’—','๐—','๐˜ƒ','๐˜ท','๐™ซ','๐“‹','๐“ฟ','๐”ณ','๐–›','๐šŸ','๐•ง'],
['w','๐ฐ','๐‘ค','๐’˜','๐—','๐˜„','๐˜ธ','๐™ฌ','๐“Œ','๐”€','๐”ด','๐–œ','๐š ','๐•จ'],
['x','๐ฑ','๐‘ฅ','๐’™','๐—‘','๐˜…','๐˜น','๐™ญ','๐“','๐”','๐”ต','๐–','๐šก','๐•ฉ'],
['y','๐ฒ','๐‘ฆ','๐’š','๐—’','๐˜†','๐˜บ','๐™ฎ','๐“Ž','๐”‚','๐”ถ','๐–ž','๐šข','๐•ช'],
['z','๐ณ','๐‘ง','๐’›','๐—“','๐˜‡','๐˜ป','๐™ฏ','๐“','๐”ƒ','๐”ท','๐–Ÿ','๐šฃ','๐•ซ'],
['A','๐€','๐ด','๐‘จ','๐– ','๐—”','๐˜ˆ','๐˜ผ','๐’œ','๐“','๐”„','๐•ฌ','๐™ฐ','๐”ธ'],
['B','๐','๐ต','๐‘ฉ','๐–ก','๐—•','๐˜‰','๐˜ฝ','โ„ฌ','๐“‘','๐”…','๐•ญ','๐™ฑ','๐”น'],
['C','๐‚','๐ถ','๐‘ช','๐–ข','๐—–','๐˜Š','๐˜พ','๐’ž','๐“’','โ„ญ','๐•ฎ','๐™ฒ','โ„‚'],
['D','๐ƒ','๐ท','๐‘ซ','๐–ฃ','๐——','๐˜‹','๐˜ฟ','๐’Ÿ','๐““','๐”‡','๐•ฏ','๐™ณ','๐”ป'],
['E','๐„','๐ธ','๐‘ฌ','๐–ค','๐—˜','๐˜Œ','๐™€','โ„ฐ','๐“”','๐”ˆ','๐•ฐ','๐™ด','๐”ผ'],
['F','๐…','๐น','๐‘ญ','๐–ฅ','๐—™','๐˜','๐™','โ„ฑ','๐“•','๐”‰','๐•ฑ','๐™ต','๐”ฝ'],
['G','๐†','๐บ','๐‘ฎ','๐–ฆ','๐—š','๐˜Ž','๐™‚','๐’ข','๐“–','๐”Š','๐•ฒ','๐™ถ','๐”พ'],
['H','๐‡','๐ป','๐‘ฏ','๐–ง','๐—›','๐˜','๐™ƒ','โ„‹','๐“—','โ„Œ','๐•ณ','๐™ท','โ„'],
['I','๐ˆ','๐ผ','๐‘ฐ','๐–จ','๐—œ','๐˜','๐™„','โ„','๐“˜','โ„‘','๐•ด','๐™ธ','๐•€'],
['J','๐‰','๐ฝ','๐‘ฑ','๐–ฉ','๐—','๐˜‘','๐™…','๐’ฅ','๐“™','๐”','๐•ต','๐™น','๐•'],
['K','๐Š','๐พ','๐‘ฒ','๐–ช','๐—ž','๐˜’','๐™†','๐’ฆ','๐“š','๐”Ž','๐•ถ','๐™บ','๐•‚'],
['L','๐‹','๐ฟ','๐‘ณ','๐–ซ','๐—Ÿ','๐˜“','๐™‡','โ„’','๐“›','๐”','๐•ท','๐™ป','๐•ƒ'],
['M','๐Œ','๐‘€','๐‘ด','๐–ฌ','๐— ','๐˜”','๐™ˆ','โ„ณ','๐“œ','๐”','๐•ธ','๐™ผ','๐•„'],
['N','๐','๐‘','๐‘ต','๐–ญ','๐—ก','๐˜•','๐™‰','๐’ฉ','๐“','๐”‘','๐•น','๐™ฝ','โ„•'],
['O','๐Ž','๐‘‚','๐‘ถ','๐–ฎ','๐—ข','๐˜–','๐™Š','๐’ช','๐“ž','๐”’','๐•บ','๐™พ','๐•†'],
['P','๐','๐‘ƒ','๐‘ท','๐–ฏ','๐—ฃ','๐˜—','๐™‹','๐’ซ','๐“Ÿ','๐”“','๐•ป','๐™ฟ','โ„™'],
['Q','๐','๐‘„','๐‘ธ','๐–ฐ','๐—ค','๐˜˜','๐™Œ','๐’ฌ','๐“ ','๐””','๐•ผ','๐š€','โ„š'],
['R','๐‘','๐‘…','๐‘น','๐–ฑ','๐—ฅ','๐˜™','๐™','โ„›','๐“ก','โ„œ','๐•ฝ','๐š','โ„'],
['S','๐’','๐‘†','๐‘บ','๐–ฒ','๐—ฆ','๐˜š','๐™Ž','๐’ฎ','๐“ข','๐”–','๐•พ','๐š‚','๐•Š'],
['T','๐“','๐‘‡','๐‘ป','๐–ณ','๐—ง','๐˜›','๐™','๐’ฏ','๐“ฃ','๐”—','๐•ฟ','๐šƒ','๐•‹'],
['U','๐”','๐‘ˆ','๐‘ผ','๐–ด','๐—จ','๐˜œ','๐™','๐’ฐ','๐“ค','๐”˜','๐–€','๐š„','๐•Œ'],
['V','๐•','๐‘‰','๐‘ฝ','๐–ต','๐—ฉ','๐˜','๐™‘','๐’ฑ','๐“ฅ','๐”™','๐–','๐š…','๐•'],
['W','๐–','๐‘Š','๐‘พ','๐–ถ','๐—ช','๐˜ž','๐™’','๐’ฒ','๐“ฆ','๐”š','๐–‚','๐š†','๐•Ž'],
['X','๐—','๐‘‹','๐‘ฟ','๐–ท','๐—ซ','๐˜Ÿ','๐™“','๐’ณ','๐“ง','๐”›','๐–ƒ','๐š‡','๐•'],
['Y','๐˜','๐‘Œ','๐’€','๐–ธ','๐—ฌ','๐˜ ','๐™”','๐’ด','๐“จ','๐”œ','๐–„','๐šˆ','๐•'],
['Z','๐™','๐‘','๐’','๐–น','๐—ญ','๐˜ก','๐™•','๐’ต','๐“ฉ','โ„จ','๐–…','๐š‰','โ„ค']
];
$replace = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
$lookup = [
'serif-normal',
'serif-bold',
'serif-italic',
'serif-bolditalic',
'sans-normal',
'sans-bold',
'sans-italic',
'sans-bolditalic',
'script-normal',
'script-bold',
'franktur-normal',
'fraktur-bold',
'monospace',
'doublestruck'
];
$map_index = array_search($alphabet, $lookup);
$split = mb_str_split($input);
$output = '';
foreach ($split as $char) {
foreach ($alphabets as $i => $letter) {
if ($letter[$map_index] === $char)
$output .= $replace[$i];
}
}
return $output;
}
$input = '๐ด๐‘™๐‘’๐‘—๐‘Ž๐‘›๐‘‘๐‘Ÿ๐‘Ž';
$output = math_symbols_to_plain_text($input, 'serif-italic');
echo $input . PHP_EOL . $output . PHP_EOL;
Yields:
๐ด๐‘™๐‘’๐‘—๐‘Ž๐‘›๐‘‘๐‘Ÿ๐‘Ž
Alejandra
If I am not wrong, you are trying to decode URL then why you are not trying to use urldecode()
follow this .PHP DOC

Arabic not urlencoding correctly

I have the string:
$str = 'ู…ุงุฌุฏ';
This need to be encoded as:
'%E3%C7%CC%CF'
But I cannot figure out how to reach this encoded string. I believe it is Windows-1256. The above encoded string is how it is being encoded by a program I have.
Does anyone know how to reach this string?
If you know you want to use Windows-1256 then all you have to do is to change the encoding of the input string (which is UTF-8) to Windows-1256. Then you apply urlencode() to the returned string and that's all.
There are several ways to change the encoding of a string in PHP. One of them (that I tested and provides the result you expect) is using iconv():
$str = 'ู…ุงุฌุฏ';
$conv = iconv('utf-8', 'windows-1256', $str);
echo(urlencode($conv));
You need to somehow split the string into its hexadecimal representation and then put a % singn in front of the hex number pairs.
<?php
$hexString = bin2hex("ู…ุงุฌุฏ");
for($i = 0; $i < strlen($hexString); $i += 2){
echo "%".substr($hexString, $i, 2);
}
?>
This will do the trick but im sure there is a more elegant way.

How to convert E5%AE%89%E5%85%A8 to this format ๅฎ‰ๅ…จ in PHP 5.1

I got a keyword var from an url like this:
search.php?keyword=%E5%AE%89%E5%85%A8
with utf8 encoding. Then I want to convert the keyword to this format:
&#23433;&#20840;
So I can use it in MySQL/PHP/
How can I achieve this?
This should work.
$html = mb_convert_encoding($_GET['keyword'], 'HTML-ENTITIES', 'UTF-8');
echo htmlspecialchars($html);
//ๅฎ‰ๅ…จ
First one is url encoded, and what you want are html entities.
$string = urldecode('%E5%AE%89%E5%85%A8'); // == ๅฎ‰ๅ…จ
$string2 = htmlentities($string); // == ๅฎ‰ๅ…จ
$string3 = htmlspecialchars($string2); // == &#x5B89;&#x5168;
The one from your question is double encoded (& got converted to &) which I assume is wrong.
23433 is decimal and equals hexadecimal x5B89. Same for the second code. For the browser, it doesn't matter if it's decimal or hexadecimal.
If you really intend double encoding, use htmlspecialchars($string); on the above code.

PHP base64 encode doesn't get decoded by Android

I have a PHP script that creates a QR code out of a username and timestamp. I need to prove the QR code came from my server so I am encrypting the username and timestamp with a private RSA key. In order to get it into the QR code I am then base64 encoding it.
I am trialling it with Android first. I can get the string out of the QR code but when I base64 decode it in Android, it returns null. After debugging it seems it's because there are two whitespaces in the string. When the decoder comes to check the illegal characters are divisible by 4, it obviously fails. Getting desperate I removed the whitespaces but then it changed the length so the calculations still didn't work out. Can I change the whitespace for a 'safe' character? Or is the particular encode/decode pair not compatible??
PHP code:
$data = base64_encode($username."`".$timestamp);
$fp = fopen("private.pem", "r");
$private_key = fread($fp, 8192);
fclose($fp);
openssl_private_encrypt($data, &$encrypted_data, $private_key);
$encrypted_data_64 = base64_encode($encrypted_data);
// create QR code
Android code:
String s = data.getStringExtra("SCAN_RESULT");
byte[] b = Base64.decode(s.toCharArray());
// b is null at this point
Base64 code that it bugs out on:is
// Check special case
int sLen = str != null ? str.length() : 0;
if (sLen == 0)
return new byte[0];
// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
// so we don't have to reallocate & copy it later.
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
if (IA[str.charAt(i)] < 0)
sepCnt++;
// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
if ((sLen - sepCnt) % 4 != 0)
return null;
PHP base64 encode uses the '+' symbol. When this gets put into a QR code, the '+' comes through as space. Replaced ' ' with '+' and it got decoded fine.

json_encode() non utf-8 strings?

So I have an array of strings, and all of the strings are using the system default ANSI encoding and were pulled from a SQL database. So there are 256 different possible character byte values (single byte encoding).
Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like \u0082?
Or is that the standard for JSON?
Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like "\u0082"?
If you have an ANSI encoded string, using utf8_encode() is the wrong function to deal with this. You need to properly convert it from ANSI to UTF-8 first. That will certainly reduce the number of Unicode escape sequences like \u0082 from the json output, but technically these sequences are valid for json, you must not fear them.
Converting ANSI to UTF-8 with PHP
json_encode works with UTF-8 encoded strings only. If you need to create valid json successfully from an ANSI encoded string, you need to re-encode/convert it to UTF-8 first. Then json_encode will just work as documented.
To convert an encoding from ANSI (more correctly I assume you have a Windows-1252 encoded string, which is popular but wrongly referred to as ANSI) to UTF-8 you can make use of the mb_convert_encoding() function:
$str = mb_convert_encoding($str, "UTF-8", "Windows-1252");
Another function in PHP that can convert the encoding / charset of a string is called iconv based on libiconv. You can use it as well:
$str = iconv("CP1252", "UTF-8", $str);
Note on utf8_encode()
utf8_encode() does only work for Latin-1, not for ANSI. So you will destroy part of your characters inside that string when you run it through that function.
Related: What is ANSI format?
For a more fine-grained control of what json_encode() returns, see the list of predifined constants (PHP version dependent, incl. PHP 5.4, some constants remain undocumented and are available in the source code only so far).
Changing the encoding of an array/iteratively (PDO comment)
As you wrote in a comment that you have problems to apply the function onto an array, here is some code example. It's always needed to first change the encoding before using json_encode. That's just a standard array operation, for the simpler case of pdo::fetch() a foreach iteration:
while($row = $q->fetch(PDO::FETCH_ASSOC))
{
foreach($row as &$value)
{
$value = mb_convert_encoding($value, "UTF-8", "Windows-1252");
}
unset($value); # safety: remove reference
$items[] = array_map('utf8_encode', $row );
}
The JSON standard ENFORCES Unicode encoding. From RFC4627:
3. Encoding
JSON text SHALL be encoded in Unicode. The default encoding is
UTF-8.
Since the first two characters of a JSON text will always be ASCII
characters [RFC0020], it is possible to determine whether an octet
stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
at the pattern of nulls in the first four octets.
00 00 00 xx UTF-32BE
00 xx 00 xx UTF-16BE
xx 00 00 00 UTF-32LE
xx 00 xx 00 UTF-16LE
xx xx xx xx UTF-8
Therefore, on the strictest sense, ANSI encoded JSON wouldn't be valid JSON; this is why PHP enforces unicode encoding when using json_encode().
As for "default ANSI", I'm pretty sure that your strings are encoded in Windows-1252. It is incorrectly referred to as ANSI.
<?php
$array = array('first word' => array('ะกะปะพะฒะพ','ะšะธั€ะธะปะปะธั†ะฐ'),'second word' => 'ะšะธั€ะธะปะปะธั†ะฐ','last word' => 'ะšะธั€ะธะปะปะธั†ะฐ');
echo json_encode($array);
/*
return {"first word":["\u0421\u043b\u043e\u0432\u043e","\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430"],"second word":"\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430","last word":"\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430"}
*/
echo json_encode($array,256);
/*
return {"first word":["ะกะปะพะฒะพ","ะšะธั€ะธะปะปะธั†ะฐ"],"second word":"ะšะธั€ะธะปะปะธั†ะฐ","last word":"ะšะธั€ะธะปะปะธั†ะฐ"}
*/
?>
JSON_UNESCAPED_UNICODE (integer)
Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0.
http://php.net/manual/en/json.constants.php#constant.json-unescaped-unicode
I found the following answer for an analogous problem with a nested array not utf-8 encoded that i had to json encode:
$inputArray = array(
'a'=>'First item - ร ',
'c'=>'Third item - รฉ'
);
$inputArray['b']= array (
'a'=>'First subitem - รน',
'b'=>'Second subitem - รฌ'
);
if (!function_exists('recursive_utf8')) {
function recursive_utf8 ($data) {
if (!is_array($data)) {
return utf8_encode($data);
}
$result = array();
foreach ($data as $index=>$item) {
if (is_array($item)) {
$result[$index] = array();
foreach($item as $key=>$value) {
$result[$index][$key] = recursive_utf8($value);
}
}
else if (is_object($item)) {
$result[$index] = array();
foreach(get_object_vars($item) as $key=>$value) {
$result[$index][$key] = recursive_utf8($value);
}
}
else {
$result[$index] = recursive_utf8($item);
}
}
return $result;
}
}
$outputArray = json_encode(array_map('recursive_utf8', $inputArray ));
json_encode($str,JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT);
that will convert windows based ANSI to utf-8 and the error will be no more.
Use this instead:
<?php
//$return_arr = the array of data to json encode
//$out = the output of the function
//don't forget to escape the data before use it!
$out = '["' . implode('","', $return_arr) . '"]';
?>
Copy from json_encode php manual's comments. Always read the comments. They are useful.

Categories