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 »