PHP: serializing and unserializing string containing escaped characters - php

How do I correctly serialize and unserialize a string containing escaped characters?
Given:
$data = "\'test\'";
$out= serialize($data);
print_r($out); // -> s:8:"\'test\'";
The problem here is, that the string length is not accepted by unserialize:
$out = 's:8:"\'test\'"';
var_dump(unserialize($out)); // -> bool(false)
But if I change the string length to 6 (ignoring the escape chars):
$out = 's:6:"\'test\'"';
var_dump(unserialize($out)); // -> string(6) "'test'"
It unserializes correctly.
What would be a good way of handling this problem?

I would try calling base64_encode() before you serialize the data and then base64_decode() after unserializing the data.
$data = "\'test\'";
$out= serialize(base64_encode($data));
var_dump(base64_decode(unserialize($out))); // -> bool(false)

Your test cases don't match, you're wrapping the string in double quotes in your first example and single quotes in the second, causing the escape character to be taken literally in the latter.
$out = '\'test\'';
is different from
$data = "\'test\'";
if you do
$data = "\'test\'";
$out= serialize($data);
print_r($out); // -> s:8:"\'test\'";
$data = unserialize($out);
print_r($data); // -> \'test\'
it will work.

The problem is, that your escape characters are being evaluated by PHP. If you want to keep them intact, you should escape them too :) Example:
$out = 's:8:"\'test\'"'; // $out = s:8:"'test'"
$out = 's:8:"\\\'test\\\'"'; // $out = s:8:"\'test\'"
var_dump(unserialize($out)); // string(8) "\'test\'"

Instead of using serialize and deserialize, try json_encode and json_decode. The latter will do the (un)escaping of the quotes for you.

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

How do I declare a base64 of a json inside an api request?

I am trying to use a base64 encoding of a json array inside an api request.
This is the json array:
{"action":"START_STREAMING","metricsList": [{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"STATUS"},":[{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"TIME"},{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"INFO"}],"duration":60}
This is my current solution:
$command = base64_encode({"action":"START_STREAMING","metricsList": [{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"STATUS"},":[{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"TIME"},{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"INFO"}],"duration":60});
$request = [
'command' => $command
];
I am pretty sure this isnt quite right. Any help?
Even though Base64 encoding is not an encryption, sometimes it can be
useful to pass data or parameters in a single string.
Base64 Encode for URL:
function b64link_encode($string){
$string = base64_encode($string);
$string = urlencode($string);
return $string ;
}
encode that data and pass
Base64 Decode for URL:
function b64link_decode($string){
$string = urldecode($string);
$string = base64_decode($string);
return $string ;
}
or you can do
see this
Certain characters in the URL are special, like + which stands for a space. To send arbitrary data via the URL, you need to URL escape it to avoid characters contained in the data being recognized as "special characters". Since you're putting the data into HTML, you also need to HTML-escape it to avoid characters in the URL-encoded data being recognized as special HTML characters. Hence:
$data = /* some data */;
$base64Data = base64_encode($data);
$urlData = urlencode($base64Data);
$htmlData = htmlspecialchars($urlData);
printf('<input type="hidden" value="%s" name="pass-it-on">', $htmlData);

php convert string hex without encode

I receive data like this:
$string = "\x01\x03\r\x08\xc0";
From this variable how to output (with echo) the same value?
For example on python, when I type:
$ print [string]
I get this
["\x01\x03\r\x08\xc0"]
Note:
Without do $string = '\x01\x03\r\x08\xc0' because I uses a server who receives data and the string variable is the data.
Can someone can help me?
when php interpreter reach the line:
$string = "\x01\x03\r\x08\xc0";
it automatically parse string between double quotes. so you can not get real string. but if you can guarantee that response you get is a valid hex, use function bin2hex(). in that case you can convert result of that function to what you want. something like:
$str = bin2hex($hex);
$real_str = '';
for($i=0; $i<strlen($str); $i+=2){
$real_str .= '\\x'.$str[$i].$str[$i+1];
}
echo $real_str;

How you encode strings like \u00d6?

This is encoded: \u00d6
This is decoded: ร–
What function I have to use to decode that string into something readable?
\u00d6asdf -> ร–asdf
To convert to UTF-8, do:
preg_replace('/\\\\u([0-9a-f]{4})/ie',
'mb_convert_encoding("&#x$1;", "UTF-8", "HTML-ENTITIES")',
$string);
Since this is the escaping used in JSON, another option would be json_decode. This would, however, also require escaping double quotes and backslashes before (except those of the \uXXXX escape sequences) and adding double quotes around the string. If, however, the string is indeed JSON-encoded and that's what originally motivated the question, the correct answer would naturally be use json_decode instead of the method above.
Normally this would be the urldecode method, but it does not apply to unicode characters, like yours. Try this one instead:
function unicode_urldecode($url)
{
preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);
foreach ($a[1] as $uniord)
{
$utf = '&#x' . $uniord . ';';
$url = str_replace('%u'.$uniord, $utf, $url);
}
return urldecode($url);
}

Unable to decode JSON stripslashed String?

Does anyone know why this happens?
var_dump(json_decode(stripslashes(json_encode(array("O'Reiley"))))); // array(1) { [0]=> string(8) "O'Reiley" }
var_dump(json_decode(stripslashes(json_encode(array("O\'Reiley"))))); // NULL
Are ' used at all by the JSON functions?
I don't know for sure, but json_last_error() should :)
My guess, though, is that json_encode() does something to the \' that the stripslashes() then breaks - e.g. add another "\" to escape the backslash.
Isn't fiddling with a json encoded string using striplslashes() before it's decoded wrong anyway?
I didn't look at it too deeply, but it looks like your code is
Taking a PHP Array and turning it into a json string
Mucking with that string
Trying to decode the mucked string as json
Think of it like this
$json_string = json_encode(array("O\'Reiley");
$json_string = stripslashes($json_string);
//it's no longer json, its just some random non-conforming string
var_dump(json_decode($json_string))
You should try without stripslashes()
$result = json_encode(striptslashes(array("O\'Reiley")));
if(json_last_error() > 0){
$result = json_encode(array("O\'Reiley"));
}

Categories