Wednesday, November 4, 2009

Unmount cdrom in Ubuntu

I wanted to remove a CD from my computer. I pressed the eject button, it didn't work. I right-clicked the CD icon and select 'Unmount' or 'Eject', it didn't work.

So, I tried to open the terminal and run this command 'umount -l /media/cdrom'..and it worked!

What is -l option? According to man page:
Lazy unmount. Detach the filesystem from the filesystem hierarchy
now, and cleanup all references to the filesystem as soon as
it is not busy anymore. (Requires kernel 2.4.11 or later.)

Remove red dot and vertical blue line in jEdit

In jEdit, there's a red dot at the end of each line, and there's a vertical blue line. How to remove them?

Go to Utilities > Global Options > Text Area
Uncheck both 'End of line markers' and 'Wrap guide'.
Done.

Sunday, November 1, 2009

Mount SMB shares

I have 2 ubuntu machines. One of the machine acts as the file server. I want to mount the shared folder to the other machine.

Firstly, install Samba File System and Samba client:
sudo apt-get install smbfs smbclient

Secondly,use smbmount command to mount the share.
Ex: The shared folder is located in 192.168.1.2 machine, and the folder name is java.
I wanted to mount it to /home/adi/java
smbmount //192.168.1.2/java /home/adi/java -o username=adi,pass=mysecretpassword

Done.

Source: http://ubuntuforums.org/showthread.php?t=280473

Get MDB2's mysql package

I installed Pear a few years ago but I only used it a few times.
This morning I decided to try the MDB2 package.

I tried the sample code from the Pear website. When I ran the code, I got this error unable to find package 'MDB2_Driver_mysql' file 'MDB2\Driver\mysql.php'

So, I had to update the package. 2 things that I had to do:
1. 'pear upgrade MDB2' to upgrade MDB2 package to the latest version
2. 'pear install MDB2_Driver_mysql' to get the mysql package

Tuesday, October 20, 2009

MySQL timestamp

When you insert a new row into a table, sometimes we need to record the time of insertion. For example, you have a table called User, which contains columns such as userName and userCreationDate.

To insert the creation date, I often used statement like this:
INSERT INTO USERS (userName,userCreationDate) VALUES ('yourusername',now())

Well, there's an easier way. You can just change the type of userCreationDate column from Date to Timestamp. MySQL will automatically update the column everytime you insert a new row. By the way, I used MySQL Administrator to modify the column type.

Now I've got a new problem. When the row is updated (ex: changing the userName), the userCreationDate is also updated to the current time. I don't want the creation date to be changed, but I can't find the reason by looking the table information in MySQL Administrator. So, let's use open the MySQL using the command prompt.

Type this command 'SHOW CREATE TABLE USERS'. You'll see something like this:
`userCreationDate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP

The problem is in the 'on update CURRENT_TIMESTAMP' statement. It means the column is automatically updated when the row is modified. To turn off this functionality, just alter the table:
ALTER TABLE USERS MODIFY COLUMN userCreationDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

That's all.

Monday, October 19, 2009

Regular expression to check email address (PHP)

Here's the regex to check email address according to totallyphp.co.uk
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$

The valid characters for the username part are: underscore,hyphen,letter from a to z, number from 0 to 9.
The valid characters for the domain part are: same as username, but it doesn't include the underscore. In addition, the last part of the domain can only contain 2 or 3 letters (ex: com,au).

What's wrong with this regular expression?
The obvious one is the last part. I own a domain name that end with 'info'. 'info' has 4 characters, so it won't match the last rule, which can only accept 2 or 3 character.

Now, let's take a look at the first part: ^[_a-z0-9-]+
Based on this part, the email address can start with an underscore, letter, number, or hyphen. However, email address can only start with a letter or number. These email address are not valid: _jim@example.com, -jim@example.com

Let's improve the regular expression.
To fix the first mistake, just change the last part from {2,3} to {2,4}.

For the second one, we have to make sure that email address must not start with an underscore or hyphen.
We have to change ^[_a-z0-9-]+ to ^[a-z0-9]+
Then, we have to include the underscore and hyphen in the next part: [_a-z0-9-]*

The complete one is: ^[a-z0-9]+[_a-z0-9-]*(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$

Update:
When I did the practice, I misplaced the hyphen position, and the regex didn't work properly. Instead of [_a-z0-9-], I wrote [_-a-z0-9]
Hyphen has a special meaning in regular expression. So it's better to 'escape' the hyphen. This one will work [_\-a-z0-9]


Sources:
http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm
http://www.melbourneit.com.au/help/index.php?questionid=1118
http://www.alertsite.com/help/RegexMatching.html

Wednesday, October 14, 2009

A quick way to create rounded corners

When you need to create a box with rounded corner, you normally need to create the rounded images using software such as photoshop. I've just found a website that can create the images quickly. Here's the link: RoundedCornr