Convert PHP object to an array recursively

In PHP, you can easily convert an object to an array by typecasting it.

$array = (array) $object;

But if you have multi-dimensional / nested objects, this will only converts the top level of the object. So instead of looping through the object and typecasting it, just use this single line code:

$array = json_decode(json_encode($object), true);

And to convert an array to an object recursively:

$object = json_decode(json_encode($array, JSON_FORCE_OBJECT), false);

note: This only works if the properties of the object you are trying to convert are declared as public


Offline Mode on Spotify Linux

Unlike other desktop version, Spotify Linux version doesn’t have Menu to toggle to offline mode (at least until this post written).

But if you use 1.0.47 or later, you should be able to toggle offline mode by pressing CTRL+SHIFT+O.
Make sure you already download all of the songs on your playlist.


Deleting Some Posts

A few days ago Google sent me email, notifying me that they have disabled my Google Adsense ad serving on this website. They said my website is not currently in compliance with their AdSense program policies, which is Hacking / Cracking content.

I can’t understand, i have NO hacking / cracking content on this site. I only posted something related to Linux or Programming, and sometimes any jokes or simple articles, but NOT hacking or cracking.
I already run this website since 2006. Have no posting since 2010, and my last post is last month about Olympic 2012 Youtube channel.
Then suddenly on August 15th, Google sent me email that i have hacking / cracking content, When my unpaid earnings reached $93.58!

How’s that possible?
If i have a hacking content, why they email me now? why not alert me on 2010 after my last post? or months before when i posted many articles. Or maybe posting their own Youtube channel is prohibited?? and they called it a hacking??

I already sent an appeal request regarding this issue, and asked them to tell me which of my post contain any hacking / cracking content. But they replied that my site is still in violation, without describing any further detail which one!

So, to resolve this i deleted many of my posts which has the lowest page view, and leave the highest rated posts.

If Google still think that i still have a violation content, well.. i don’t have any clue, maybe i’ll delete my Adsense account, and never use it again!


Age of Empire II color issue on Windows 7

There’s a color issue with Age of Empires 2 on Windows 7. You will get a corrupted color in game. Its not a driver issue or some directx compatibility. I think its just because of the Aero theme’s poor handling of colors.

So how to fix it? simply..

  1. Change to Windows Classic Theme
  2. Open the resolution changer window (Right click on your desktop, and select “Change Resolution”)
  3. Run Age of Empire game.

Now the color problem has gone.

Sounds silly?? nope.. IT WORKS!
Believe me, it really works..


PHP-GD: Create Image from Text

Some people want to hide their text into image for any reason. As example, to avoid spambot steal their email address, or to generate dynamic images content.

Its simply done with PHP and GD Library. See this simple function:

function imgfromstring($string){
	$font  = 5;
	//dynamic size of image. to fit your text.
	$width  = ImageFontWidth($font) * strlen($string);
	$height = ImageFontHeight($font);

	//start creating the image
	$gbr = imagecreate ($width,$height);

	//set background to white
	$bgcolor = imagecolorallocate($gbr, 255, 255, 255);
	//set color for text
	$txtcolor = imagecolorallocate ($gbr, 0, 0, 0);

	//generate the image
	imagestring($gbr, $font, 0, 0, $string, $txtcolor);
	header("Content-type: image/jpeg");
	return imagejpeg($gbr);

$txt = "";
echo imgfromstring($txt);

example output:

Another sample is using image as the background. Its useful when you need to create waterwark to protect your images.
Sample code:

function toImgWithBG($str){
	$bg = "background.jpg";
	$font = 5; //Font size

	$gbr =imagecreatefromjpeg($bg);

	//Calculate string width and height
	$pos_x  = imagesx($gbr) - (ImageFontWidth($font) * strlen($str));
	$pos_y = imagesy($gbr) - ImageFontHeight($font);

	//set text color
	$teks = imagecolorallocate($gbr, 255, 0, 0);

	//generate the image, and put on the right bottom
	imagestring($gbr, 5, $pos_x, $pos_y, $str, $teks);

	header("Content-type: image/jpeg");
	return imagejpeg($gbr);

echo toImgWithBG("copyright:");

example output:

You can download source code of this example here:
download here download this file


Create Permanent Static Route with Two Lan Card

I want to create routing for my two Lan Card. First Lan Card (eth0) for Internet access, and second Lan Card (eth1) for my Intranet. So when i access internet, it will route to my eth0 by default. otherwise, with eth1 for intranet access.

Public (eth0):

Intranet (eth1):

Basic command for adding route on Linux:

route add [-net|-host] <IP/Net> netmask <Mask> gw <Gateway IP> dev <Int>X

And this is what we do:

route add -net netmask gw dev eth1
route add -net default netmask gw dev eth0

To check the route, simply type “route” with no arguments


The output will seen like this:

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface * U 1 0 0 eth0 * U 1 0 0 eth1
link-local * U 1000 0 0 eth0 UG 0 0 0 eth1
default UG 0 0 0 eth0

Now, to keep this route persistent, add this lines to file /etc/networking/interfaces

up route add -net netmask gw dev eth1
up route add -net default netmask gw dev eth0

Restart networking to confirm the changes

sudo /etc/init.d/networking restart

Other method, Create a file called defaultroute in /etc/init.d and make it executable (chmod +x /etc/init.d/defaultroute).
Add the route commands there and do this:

update-rc.d defaultroute defaults 99



Restore Ubuntu Boot Loader

To repair your Ubuntu GRUB, you need Ubuntu LiveCD, boot from this CD, and open a terminal. Be a root, by typing “sudo su” and enter your password.

Find the partition which your Ubuntu system is installed.

# fdisk -l

On my machine, my Ubuntu partition is on /dev/sda3, so i’ll use this for example.

Create a mountpoint for this:

# mkdir /media/rootgrub
# mount /dev/sda3 /media/rootgrub

Check if your partition is the correct partition

# ls /media/rootgrub

if the output is not like this, then you have the wrong partition

bin dev home lib mnt root srv usr boot etc initrd lib64 opt sbin sys
var cdrom initrd.img media proc selinux tmp vmlinuz

unmount the partition if have the wrong one, then mount the others. To make sure you have the correct partition, run ls /media/root/boot, which should output something like this :

config-2.6.18-3-686 initrd.img-2.6.18-3-686.bak
grub lost+found vmlinuz-2.6.18-3-686 initrd.img-2.6.18-3-686

Now when everything is ok, install the Grub:

# sudo grub-install –root-directory=/media/rootgrub /dev/sda

If you have some warning, try this:

# sudo grub-install –root-directory=/media/rootgrub /dev/sda –recheck

Change /dev/sda to other partition you want to install Grub on. If all goes fine, you will see the output like this:

Installation finished. No error reported. This is the contents of the device map /boot/grub/ Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install’.

(hd0) /dev/sda

Restart to your harddrive (not to LiveCD) and see, your GRUB should be AUTOMAGICALLY appear!