Sunday, July 13, 2008

Regular Expressions for username, date, ip address, color, postal code, and phone

Pasting here just to reuse them in the near future.

Here are eight examples of practical PHP regular expressions and techniques that I've used over the past few years using Perl Compatible Regular Expressions. This guide goes over the eight different validation techniques and describes briefly how they work. Usernames, telephone numbers, email addresses, and more. Here are eight examples of practical PHP regular expressions and techniques that I've used over the past few years using Perl Compatible Regular Expressions. This guide goes over the eight different validation techniques and describes briefly how they work. Usernames, telephone numbers, email addresses, and more.

Validating Usernames

Something often overlooked, but simple to do with a regular expression would be username validation. For example, we may want our usernames to be between 4 and 28 characters in length, alpha-numeric, and allow underscores.
$string = "userNaME4234432_";
if (preg_match('/^[a-z\d_]{4,28}$/i', $string)) {
echo "example 1 successful.";
}

Validating Telephone Numbers

A much more interesting example would be matching telephone numbers (US/Canada.) We'll be expecting the number to be in the following form: (###)###-####
$string = "(032)555-5555";
if (preg_match('/^(\(?[2-9]{1}[0-9]{2}\)?|[0-9]{3,3}[-. ]?)[ ][0-9]{3,3}[-. ]?[0-9]{4,4}$/', $string)) {
echo "example 2 successful.";
}

Thanks to Chris for pointing out that there are no US area codes below 200.

Again, whether the phone number is typed like (###) ###-####, or ###-###-#### it will validate successfully. There is also a little more leeway than specifically checking for enough numbers, because the groups of numbers can have or not have parenthesis, and be separated by a dash, period, or space.

Email Addresses

Another practical example would be an email address. This is fairly straightforward to do. There are three basic portions of an email address, the username, the @ symbol, and the domain name. The following example will check that the email address is in the valid form. We'll assume a more complicated form of email address, to make sure that it works well with even longer email addresses.
$string = "first.last@domain.co.uk";
if (preg_match(
'/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',
$string)) {
echo "example 3 successful.";
}

Postal Codes

Validating Postal codes (Zip codes?,) is another practical example, but is a good example to show how ? works in regular expressions.

$string = "55324-4324";
if (preg_match('/^[0-9]{5,5}([- ]?[0-9]{4,4})?$/', $string)) {
echo "example 4 successful.";
}

What the ? does in this example is saying that the extra 4 digits at the end can either not exist, or exist- but only once. That way, whether or not they type them in, it will still validate correctly.

IP Addresses

Without pinging or making sure it's actually real, we can make sure that it's in the right form. We'll be expecting a normally formed IP address, such as 255.255.255.0.
$string = "255.255.255.0";
if (preg_match(
'^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$',
$string)) {
echo "example 5 successful.";
}

Hexadecimal Colors

Moving right along with numbers, we could check for Hexadecimal color codes, in short hand or long hand format (#333, 333, #333333 or 333333) with an optional # symbol. This could be useful in a lot of different ways... maybe previewing CSS files? Grabbing colors off pages? The options are endless.
$string = "#666666";
if (preg_match('/^#(?:(?:[a-f\d]{3}){1,2})$/i', $string)) {
echo "example 6 successful.";
}

Multi-line Comments

- A simple way to find or remove PHP/CSS/Other languages multi-line comments could be useful as well.
$string = "/* commmmment */";
if (preg_match('/^[(/*)+.+(*/)]$/', $string)) {
echo "example 7 successful.";
}

Dates

- And my last simple, yet practical example would be dates, in my favorite MM/DD/YYYY format.
$string = "10/15/2007";
if (preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $string)) {
echo "example 8 successful.";
}



The source is here.

Tuesday, July 8, 2008

firefox 3.0 crashes

As soon as Firefox 3.0 have been officially released, i downloaded and installed it. But i have many complains on this new version. It consumes more than 50% resources on PC, and crashed 3-5 times a day. Especially when working with heavy ajax applications like Google Spreadsheet, or similar. I have sent multiple crash reports to Firefox team, and hoping that they are working on it.

Planning to move back to FF 2.0

Sunday, July 6, 2008

Outsource your Symfony and Zend framework applications

It is been quite long time since we have chose Symfony and Zend framework as a PHP5 development. I was wondering how we were developing using plain PHP. It was all pain! The only best way was to create our custom framework and use it on all projects. These frameworks are the best practice and result of what PHP developers used in the past to create their web applications. We now focused on the web application outsourcing under the company Singleton LLC, which i founded a year ago. Our team is now capable of building small to medium sized web applications using these framework. Having developed tens of projects, we now see that we should really be different from other teams in India, or Pakistan. We should develop in such a way not to earn money in a shortest time, but to work in a detail-oriented ways to deliver what customer exactly wished. To deliver running application is not the final result, and it shouldn't be the outsourcing goal even for small teams like us. So we really focused on details, and comminication. We understood that we need more than just a coding.

On the other side, the are buyers who wish to have their software codes in a cheapest and shortest way, while the others are looking for someone who can implement their ideas in quite cheap but qualified way. We'd rather to work with the companies who are looking for qualified team, but the quite reasonable price. Our aim is not to become the cheaper than others, but more qualified.

If you are looking for a responsive and responsible team to implement your ideas into real life, we are available for hire. You can outsource your Symfony and Zend Framework projects to us.

Thanks,
Bayar