Answer the question
In order to leave comments, you need to log in
How to encode a string into a unicode sequence?
I ran into a problem that I can't solve. PHP programming language.
You need to encode a string into a Unicode sequence.
There is a line with value
"W+I4zLZyeJDAhQMe33tYRcXC+Falj9baMfjJw5yOijaPNHDYcnNlRlTGm3rvA6cn2hXMmW4ZKEyTphwzNMQlOlnNiCHK2LLpJLgXK6JHKuTYeParwqCud5Vn2dZXpxctrQf8OkbCqF12u0GcBR2or2HFBrEV13Jd5YAX4bRbUBk="
"\u0057\u002b\u0049\u0034\u007a\u004c\u005a\u0079\u0065\u004a\u0044\u0041\u0068\u0051\u004d\u0065\u0033\u0033\u0074\u0059\u0052\u0063\u0058\u0043\u002b\u0046\u0061\u006c\u006a\u0039\u0062\u0061\u004d\u0066\u006a\u004a\u0077\u0035\u0079\u004f\u0069\u006a\u0061\u0050\u004e\u0048\u0044\u0059\u0063\u006e\u004e\u006c\u0052\u006c\u0054\u0047\u006d\u0033\u0072\u0076\u0041\u0036\u0063\u006e\u0032\u0068\u0058\u004d\u006d\u0057\u0034\u005a\u004b\u0045\u0079\u0054\u0070\u0068\u0077\u007a\u004e\u004d\u0051\u006c\u004f\u006c\u006e\u004e\u0069\u0043\u0048\u004b\u0032\u004c\u004c\u0070\u004a\u004c\u0067\u0058\u004b\u0036\u004a\u0048\u004b\u0075\u0054\u0059\u0065\u0050\u0061\u0072\u0077\u0071\u0043\u0075\u0064\u0035\u0056\u006e\u0032\u0064\u005a\u0058\u0070\u0078\u0063\u0074\u0072\u0051\u0066\u0038\u004f\u006b\u0062\u0043\u0071\u0046\u0031\u0032\u0075\u0030\u0047\u0063\u0042\u0052\u0032\u006f\u0072\u0032\u0048\u0046\u0042\u0072\u0045\u0056\u0031\u0033\u004a\u0064\u0035\u0059\u0041\u0058\u0034\u0062\u0052\u0062\u0055\u0042\u006b\u003d"
var myString = "W+I4zLZyeJDAhQMe33tYRcXC+Falj9baMfjJw5yOijaPNHDYcnNlRlTGm3rvA6cn2hXMmW4ZKEyTphwzNMQlOlnNiCHK2LLpJLgXK6JHKuTYeParwqCud5Vn2dZXpxctrQf8OkbCqF12u0GcBR2or2HFBrEV13Jd5YAX4bRbUBk=";
var unicodeString = string.Join("", myString.Select(c => "\\u" + ((int)c).ToString("x4")));
return unicodeString;
Answer the question
In order to leave comments, you need to log in
If you are sure that only a-zA-Z0-9-=+- (similar to base64) will be in the input, then you can simplify and not re-encode, like this:
$uncomingString = 'W+I4zLZyeJDAhQMe33tYRcXC+Falj9baMfjJw5yOijaPNHDYcnNlRlTGm3rvA6cn2hXMmW4ZKEyTphwzNMQlOlnNiCHK2LLpJLgXK6JHKuTYeParwqCud5Vn2dZXpxctrQf8OkbCqF12u0GcBR2or2HFBrEV13Jd5YAX4bRbUBk=';
function forceUnicode( $str = '' )
{
$result = '';
for( $i=0,$length=strlen($str); $i<$length; $i++)
{
$result .= "\\u".str_pad(bin2hex($str[$i]), 4, '0', STR_PAD_LEFT);
}
return $result;
}
echo forceUnicode($uncomingString);
/*
Выведет:
\u0057\u002b\u0049\u0034\u007a\u004c\u005a\u0079\u0065\u004a\u0044\u0041\u0068\u0051\u004d\u0065\u0033\u0033\u0074\u0059\u0052\u0063\u0058\u0043\u002b\u0046\u0061\u006c\u006a\u0039\u0062\u0061\u004d\u0066\u006a\u004a\u0077\u0035\u0079\u004f\u0069\u006a\u0061\u0050\u004e\u0048\u0044\u0059\u0063\u006e\u004e\u006c\u0052\u006c\u0054\u0047\u006d\u0033\u0072\u0076\u0041\u0036\u0063\u006e\u0032\u0068\u0058\u004d\u006d\u0057\u0034\u005a\u004b\u0045\u0079\u0054\u0070\u0068\u0077\u007a\u004e\u004d\u0051\u006c\u004f\u006c\u006e\u004e\u0069\u0043\u0048\u004b\u0032\u004c\u004c\u0070\u004a\u004c\u0067\u0058\u004b\u0036\u004a\u0048\u004b\u0075\u0054\u0059\u0065\u0050\u0061\u0072\u0077\u0071\u0043\u0075\u0064\u0035\u0056\u006e\u0032\u0064\u005a\u0058\u0070\u0078\u0063\u0074\u0072\u0051\u0066\u0038\u004f\u006b\u0062\u0043\u0071\u0046\u0031\u0032\u0075\u0030\u0047\u0063\u0042\u0052\u0032\u006f\u0072\u0032\u0048\u0046\u0042\u0072\u0045\u0056\u0031\u0033\u004a\u0064\u0035\u0059\u0041\u0058\u0034\u0062\u0052\u0062\u0055\u0042\u006b\u003d
*/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question