Code to Text (PHP, languages) - php

When I submit a post with AJAX that is not in English, I will get something similar to %u4F60%u662F%u5982%u4F55%u505A%uFF1F
What can I do to fix this in PHP? I've tried utf8_decode and doesn't work.
I'm submitting the text with AJAX if that helps.

Does this do what you want?
<?php
function utf8_urldecode ($str) {
return urldecode(preg_replace_callback('/%u([0-9A-F]{4,6})/i', function($matches) {
$int = hexdec($matches[1]);
switch (TRUE) {
case $int < 0x80:
return pack('C*', $int & 0x7F);
case $int < 0x0800:
return pack('C*', (($int & 0x07C0) >> 6) | 0xC0, ($int & 0x3F) | 0x80);
case $int < 0x010000:
return pack('C*', (($int & 0xF000) >> 12) | 0xE0, (($int & 0x0FC0) >> 6) | 0x80, ($int & 0x3F) | 0x80);
case $int < 0x110000:
return pack('C*', (($int & 0x1C0000) >> 18) | 0xF0, (($int & 0x03F000) >> 12) | 0x80, (($int & 0x0FC0) >> 6) | 0x80, ($int & 0x3F) | 0x80);
default:
return $matches[0];
}
}, $str));
}
$str = "%u4F60%u662F%u5982%u4F55%u505A%uFF1F";
echo utf8_urldecode($str);
I have never tried to convert hex UTF-8 code points to binary before, turns out it's actually quite easy when you get your head around it. Of course, it may still display as nonsense in your browser, depending on what the characters actually are - you may need install a language pack for them to render correctly.

Related

Equivalent to java "Color.decode" in php or mysql

I have a mysql database with a string field called "color" from java
I keep the value so:
// (2=color field)
pst.setString (2, objeto.getBackground (). getRGB ())
and then I read it like this:
objeto.setBackground (Color.decode (rs.getString ("color")))
Is there any function in mysql or php to read the color?
What do you want exactly?
Convert a int to a color in #RRGGBB format?
sprintf('#%06X', $color);
or get the R, G, B values?
$r = ($color >> 16) & 0xFF;
$g = ($color >> 8) & 0xFF;
$b = ($color >> 0) & 0xFF;
or create a int from rgb values
$color = ($r & 0xFF) << 16 | ($g & 0xFF) << 8 | $b & 0xFF

Swapping bytes of text in PHP

I basically need to port this piece of code to php
for (i = 0; i < 128/4; i++)
data32[i] = bswap_32(data32[i]);
But, there is no bswap function in php.
Would someone be kind enough to provide me with something that could solve the problem?
This should do it (untested):
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (($j & 0xff000000) >> 24);
}
Or, if there is a sign extension problem, this should resolve it:
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (255 & (($j & 0xff000000) >> 24));
}
It sounds like bswap_32 is swapping endianness of your 32-bit quantities.
I could just give you some code, but I'd prefer not to do people's work for them, so I'll explain the principle instead:
You can achieve that with bit-shifts and masks (so for instance, you need to mask out the 8 lowest bits, and shift them into the highest 8 bit positions of the result).
Shifting can be done with the << and >> operators. Masking can be done with the & operator. See the PHP manual page on operators for more details.

PHP equivalent of C code from Bit Twiddling Hacks?

http://www-graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
v = v - ((v >> 1) & (T)~(T)0/3); // temp
v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp
v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp
c = (T)(v * ((T)~(T)0/255)) >> (sizeof(v) - 1) * CHAR_BIT; // count
This is the same problem in Python: Python equivalent of C code from Bit Twiddling Hacks?
I need to use this code in PHP, independently from integer size (the above code works up to 128-bit integers, which will do fine for me). Here's what I tried:
function countSetBits($int) {
$mask = (1 << PHP_INT_SIZE*8) - 1;
$int = $int - (($int >> 1) & (int) $mask/3);
$int = ($int & ((int) $mask/15)*3) + (($int >> 2) & ((int) $mask/15)*3);
$int = ($int + ($int >> 4)) & ((int) $mask/255)*15;
return ($mask & $int * ((int) $mask/255)) >> ((int) PHP_INT_SIZE - 1) * 8;
}
The reason this doesn't work (on a 64-bit machine with 64-bit PHP - Debian Squeeze) is that PHP doesn't seem to support 64-bit unsigned integers (how to have 64 bit integer on PHP?). I'm afraid I will have to use an arbitrary-precision math library. Or is there another way?
For now, this is what I used:
function countSetBits($int) {
return substr_count(base_convert($int, 10, 2), '1');
}
Try to use the following in your php script before 64-bit operations:
ini_set('precision', 20);

PHP Bit shifting in code?

Can someone help me understand this ancient code?
$a = 00040000000002;
$n = sscanf($a,"%02x%02x%02x%02x%02x%02x%02x",$r[7],$r[6],$r[5],$r[4],$r[3],$r[2],$r[1]);
$ptemp = $r[1] + (($r[2] & 0x1F) << 8);
$l[$i] = (($r[2] & 0xE0) >> 5) + ($r[3] << 3);
$m[$i] = $r[4] + (($r[5] & 0x03) << 8);
$h[$i] = (($r[5] & 0xFC) >> 2) + (($r[6] & 0x03) << 6);
$dist[$i] = (($r[6] & 0xFC) >> 2) + (($r[7] & 0x0F) << 6);
$fruittoday[$i] = ($r[7] & 0xF0) >> 4;
I understand the sscanf, but I'm not sure what is going on with the & 0x1f << 8, etc.
Any ideas?
They're using Bitwise Operators to check if specific bits are turned on.
In the example you've provided, they're confirming the first 5 bits are 1's then shifting all bits to the left 8 places. (Although it seems like in this case they're starting with 0 (00000000), checking if 0x1F are on (00011111) which would still be 0, then shifting them left 8 places (put in to the 2^8th position)
For reference, I would check out php's manual on bitwise operators. They list some example of what's going on here. If you're really curious, just echo the variables as they are manipulated (or break it out in to separate operations) and then examine them).

PHP Bytes 2 DWord

I have an array:
$arr[0] = 95
$arr[1] = 8
$arr[2] = 0
$arr[3] = 0
That are bytes. I need a DWORD.
I tried:
$dword = $arr[0]+$arr[1]*265+$arr[2]*265*265+$arr[3]*265*265*265;
Is that right or am I doing it wrong?
Try:
$dword = (($arr[3] & 0xFF) << 24) | (($arr[2] & 0xFF) << 16) | (($arr[1] & 0xFF) << 8) | ($arr[0] & 0xFF);
It can also be done your way with some corrections:
$dword = $arr[0] + $arr[1]*0x100 + $arr[2]*0x10000 + $arr[3]*0x1000000;
Or using pack/unpack:
$dword = array_shift(unpack("L", pack("CCCC", $arr[0], $arr[1], $arr[2], $arr[3])));
Or try<?php
$arr = array(95,8,0,0);
$bindata = join('', array_map('chr', $arr));
var_dump(unpack('L', $bindata));both (Emil H's and my code) give you 2143 as the result.
Or at the very least use 256 rather than 265.
Your code should work correctly, but you should multiply with 256, not 265. (in 8 bits, there are 2^8 = 256 unique values). It works, because multiplying with 256 is the same as shifting the bits 8 places to the left.
Perhaps you should consider using the bitwise operators instead, to better convey the intent. See http://theopensourcery.com/phplogic.htm

Categories