Monthly Archives: January 2014

Sort an array of objects in PHP by field

function cmp($a, $b)
{
    return strcmp($a->name, $b->name);
}

usort($your_data, "cmp");

Or the shorter version using closures:

usort($your_data, function($a, $b)
{
    return strcmp($a->name, $b->name);
});

Ref : http://php.net/manual/en/function.usort.php
http://stackoverflow.com/questions/4282413/php-sort-array-of-objects-by-object-fields

Use sudo without being asked for your password

Run in a terminal:

sudo visudo

The sudoers file will be opened with your default editor. There add this line at the end of the file:

username ALL=(ALL) NOPASSWD: ALL

Then safe and exit.

Backup and restore hard drive with “dd”

Look for the hard drives device with mount or sudo fdisk -l and unmount it. In my case it’s going to be sda.

Backup uncompressed image:

sudo dd if=/dev/sda of=/home/sda.bin bs=1024

Backup compressed image:

sudo dd if=/dev/sda bs=1024 | gzip > /home/user/sda.bin.gz

If the target file system where the image is going to be created can’t have files larger than 4GB for example (as for FAT32) you’ll have to split the output:

sudo dd if=/dev/sda conv=sync,noerror bs=64K | gzip -c | split -b 2000m - /media/usbdrive/sda.bin.gz

Restore uncompressed image:

sudo dd if=/home/user/sda.bin of=/dev/sda bs=1024

Restore compressed image:

sudo gzip -dc /home/user/sda.bin.gz | dd of=/dev/sda bs=1024

Restore compressed and splited image:

sudo cat /media/usbdrive/sda.bin.gz.* | gzip -dc | dd of=/dev/sda conv=sync,noerror bs=64K

You can do the same with only one partition of the hard drive if you replace sda with sdaX being X the partition number.

Ref: https://help.ubuntu.com/community/DriveImaging#Creating_Disc_Images_Using_dd
http://ubuntuforums.org/showthread.php?t=1540873

Symfony2 routing: Allow dots in URL

I have one route in my app that must have a text and a dot followed by a format. For example:

http://domain.com/image/mi-text-with-dots-.-in-.-the-.-middle.png

As you can see it’s an url that can contain dots before the dot that splits the {text} and the {_format} parts.

To make this work you have to define the route in routing.yml using a regexp for {text} this way:

dotted_url:
    pattern:  /image/{text}.{_format}
    defaults: { _controller: AcmeDemoBundle:Demo:image, _format: png }
    requirements:
        _format: png|jpg
        text: .+

Ref: http://stackoverflow.com/questions/18098583/error-in-symfony2-when-url-cointains-dot