Tuesday, November 29, 2011

PHP to Convert Binary Data (in String Value) to Numeric Values



$string = "10101011";
echo(stringbin2num($string)); // Will return 171

function stringbin2num($input) {

 $len = strlen($input);
 $count = 0; $val = 0;

 while ($count < $len) {

  if (substr($input, $len-$count-1, 1)) {
   $val += pow(2, ($count));
  }
  $count++;
 }

 return $val;
}



This function might not be needed in many situations. It's just a reference for those who want to convert string to numbers. You can also modify these codes to handle string of HEX characters such as 'AABB'. If you are reading strings from the binary file, the best is still to use the unpack() function. Unpack function allows you convert to many formats you like.
Read More »

PHP to Write Binary Data (or Array Data) to a File using Pack Function




Writing the Binary Data directly using PHP's Pack() function:



// Write Binary Data
$bin_data = pack("C*", 0x47, 0x49, 0x46);

$fp = fopen("c://test.bin", 'wb'); // replace with your filename
// (please also check file permission before write)
fwrite($fp, $bin_data);
fclose($fp);







Writing the Binary Data from an Array (if the data length is too long):



// Write Binary Data
$bin_data_array = array(0x47, 0x49, 0x46, .....................................);

foreach ($bin_data_array as $data) {

$bin_data .= pack("C", $data);

}

$fp = fopen("c://test.bin", 'wb'); // replace with your filename
// (please also check file permission before write)
fwrite($fp, $bin_data);
fclose($fp);









This WON'T work:



// Write Binary Data
$bin_data_array = array(0x47, 0x49, 0x46, .....................................);

$bin_data = pack("C*", $bin_data_array);

$fp = fopen("c://test.bin", 'wb'); // replace with your filename
//(please also check file permission before write)
fwrite($fp, $bin_data);
fclose($fp);






Read More »

Facebook Funny Little Trick

There are numerous posts I find it funny.

It is using the Facebook wall to post something funny using the UID such as

@[84786553145:0]

After you paste on Facebook wall and it will appear as "Kiss Me!".

It's actually showing the username of the Facebook page "Kiss Me!" and people may not be aware of this when they paste it onto their wall.

But you need to add something like '+' sign to something like @+[84786553145:0] when you send the instruction to paste it to your friends wall and tell them to remove the '+' sign before posting. Otherwise, the "Kiss Me!" will appear in your instruction to your friend instead of the trick codes.

Basically here's the syntax to post username on Facebook wall:

@[Facebook UID:Any Number]
Read More »

Tuesday, November 22, 2011

Global Variable/Array in PHP

The global variable is declared in the function instead of in the main as in C language.


<?PHP

$my_global_array = array(); // Global array set up in main
$my_global_variable; // Global variable set up in main
.
.
.


funtion a() {
global $my_global_array, $my_global_variable;
// declare the array and variable in main as global variable
.
.
.

}

function b() {
global $my_global_array;
// only takes this global array as the global variable is not needed in this function
.
.
.

}


?>





Read More »

Monday, November 21, 2011

CSS font used by Facebook

Now the fonts used by Facebook become the favorite for many. If you do a view source, you'll see this CSS setting:


font-family:"lucida grande",tahoma,verdana,arial,sans-serif;
font-size: 11px;


The color tone is not entirely black and it is a bit towards grey.
Read More »

PHP: Setting and Reading Cookies



<?PHP

setcookie("myCookieName", "myCookieValue");

// To read cookie, simply retrieve from the $_COOKIE['myCookieNameToRead']

.
.
.


?>


<HTML>
.
.



IMPORTANT: Make sure your <PHP is the first thing is your PHP file. Even a single tiny white space character prior to <PHP will also render the cookie operation useless.
Read More »

PHP: $_REQUEST or $_POST or $_GET?





If you are very sure of the return string type, use $_POST['myStringKey'] for POST data and $_GET['myStringKey'] for GET data.

If you are not really sure (ex. it's from a third party source such as Facebook), use $_REQUEST['myStringKey'].




Read More »

PHP Weird Initialization Criteria

It is weird that before pushing data to an array, one needs to initialize the array before that.

Here's what I did to avoid this weird error (pushed data will be missing):



$my_array = array(); // initialize $my_array is of array type

array_push($my_array, "myTestData1");



If you are used to PERL, this is weird.
Read More »

The Unbelievable PHP Debug Feature: print_r()

There is a function that I personally like very much: print_r().

It prints out the raw data inside a string or array. Especially when dealing nested arrays. It is a function I cannot live without.

When dealing JSON data return from Facebook API/Graph, print_r() can be a very powerful debugging feature especially when the returning data structure is unknown.

Here's an example of examining data returned from Facebook FQL engine:



// Get your access token ($access_token) from OAuth 2.0 procedure

$fql_query_url = 'https://graph.facebook.com/' . '/fql?q=
SELECT+uid,name+FROM+user+WHERE+uid+IN+
(SELECT+uid2+FROM+friend+WHERE+uid1+=+me())' . '&' . $access_token;

// FQL query to get all the friends (UID and name) of a facebook user

$user_data = json_decode(file_get_contents($fql_query_url));

print_r($user_data);



Since the returning data structure can be very "nested" (try add in location, hometown, education, ... to the query, you'll see) and uncertain, print_r() is the way to go.

P.S.: Facebook FQL will return a empty JSON data if the return data size is more than certain bytes set by Facebook.
Read More »

PHP Codes to Force a Number to Be a Negative Number



$number = ~abs($number) + 1;




~ is to invert all the bits in PHP and + 1 is to make it a negative number. Remember in CS101 that binary number can be converted to represent a negative number by invert all the bits and +1?

abs() is to make sure it is not a negative number and force it to be a positive number even though it is a negative number. Inverting a negative number will only make it worse.
Read More »

Use JQUERY to blink Texts



<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
function startBlink(){
window.blinker = setInterval(function(){
if (window.blink) {
$('.blink').css('color','black');
window.blink=false;
}
else {
$('.blink').css('color','white');
window.blink = true;
}
},500);
}

startBlink();

</script>

...
<span class="blink"> Your texts here! </span>



This jquery assumes your background color is white and text color is black. You can change the appearance accordingly to suit your need. The interval is set at 500ms. Adjust the timing accordingly.
Read More »

Sunday, November 20, 2011

The Facebook Avatar Thumb Nail URL




http://graph.facebook.com/[Facebook Profile ID]/picture




This link will be redirected to the actual jpg file in one of the servers of facebook.
Read More »

PHP Code to Convert Unsigned Word (Double Byte, 16-bit String) to Signed Integer

This is useful when one tries to read a two 8-bit unsigned char from binary file and then convert to a signed integer.

The following function is good to convert from 2 character byte string such as 0A, FE, etc., to a signed integer (tested working on both 32-bit and 64-bit Windows 7 IIS environment):




function read_short($s) { // assume $s is an array of (char 1, char 2)

$temp = (($s[0]&0x00ff)<<8) + ($s[1]&0x00ff);

if ($temp & (1 << 15) ) {
$temp = ($temp - (1 << 16)); // fill all bits with 1 from bit 17 onwards
}

return $temp;
}





This is the trick to process your 16-bit char (two unsigned char) in 32-bit/64-bit windows PHP environment.
Read More »