My problem is about string decoding.
Let's assume a string like:
$str = "\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6";
I want to decode it and to look like that:
λƛ¬∧⟑∨⟇÷ €½∆ø↔¢⌐æ
I tried to use
utf8_encode(utf8_encode($str));
But it's not what was expected.
In python something like that works:
_str = b"\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6"
_str = _str.decode()
print(_str)
You don't need to decode that. This is legal notation for strings in PHP.
$str = "\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6";
echo $str; //λƛ¬∧⟑∨⟇÷ €½∆ø↔¢⌐æ
https://3v4l.org/0e0Po
Related
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;
What is the php function to use so as to convert this
%25CE%2592%25CE%2595%25CE%259D%25CE%2596%25CE%2599%25CE%259D%25CE%2597
to the actual string using PHP?
The actual string is written in Greek characters and it is "ΒΕΝΖΙΝΗ"
Your string is encoded two times.
You can decode it with rawurldecode:
$str = rawurldecode('%25CE%2592%25CE%2595%25CE%259D%25CE%2596%25CE%2599%25CE%259D%25CE%2597');
// $str = "%CE%92%CE%95%CE%9D%CE%96%CE%99%CE%9D%CE%97"
$str = rawurldecode($str);
// $str = "ΒΕΝΖΙΝΗ"
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.
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:
安全
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); // == 安全
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.
How can i convert string like this
%C4%BE%C5%A1%C4%8D%C5%A5%C5%BE-%C3%BD%C3%A1%C3%AD%C3%A9
to this:
ľščťž-ýáíé
using PHP ?
Just use urldecode() function, because you have url encoded string.
$encoded = '%C4%BE%C5%A1%C4%8D%C5%A5%C5%BE-%C3%BD%C3%A1%C3%AD%C3%A9';
$decoded = urldecode($encoded);
echo $decoded;
outputs:
ľščťž-ýáíé
Just use urldecode() function in php. Use the code below
<?php
$string = "%C4%BE%C5%A1%C4%8D%C5%A5%C5%BE-%C3%BD%C3%A1%C3%AD%C3%A9";
echo urldecode($string); // Outputs ľščťž-ýáíé
?>
Hope this helps you