Practical PHP Regular Expressions
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.";
}
Thanks to Dave Doyle for correcting and improving the username, zip code, IP address, and date regular expressions.
These are just some examples of the Regular Expressions I've written to “get the job done” for quite awhile. They work well for the uses in which I've needed them, and hopefully they'll be of some use to you as well.
Have some regular expressions you're having a problem with? Check out our Guide for PHP Regular Expressions and PCRE Tester and Cheat Sheet. Looking for a regular expression to do something particular? Leave a comment, I'd love to hear what you have to say, and would love to hear some of your ideas for other regular expressions.
CURL, a simple way to send HTTP Requests
Definition: CURL
curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.
Ref: http://curl.haxx.se/
Using CURL in XAMPP
There appear to be a lot of misguided people on the intarwebs claiming all sorts of varying things you have to do to get CURL to work on a Windows-based XAMPP install. I’d like to clear them all up here and now.
It’s really quite simple - uncomment extension=php_curl.dll in your php.ini file available in the Apache\bin directory, then restart Apache
Ref: http://incoherentbabble.com/2007/04/21/using-curl-in-xampp/
Example
//initialize the request variable
$request = "";
//this is the username of our TM4B account
$param["username"] = "abcdef";
//this is the password of our TM4B account
$param["password"] = "12345";
//this is the message that we want to send
$param["msg"] = "This is sample message.";
//these are the recipients of the message
$param["to"] = "447768254545|447956219273|447771514662";
//this is our sender
$param["from"] = "MyCompany";
//we want to send the message via first class
$param["route"] = "frst";
//we are only simulating a broadcast
$param["sim"] = "yes";
//traverse through each member of the param array
foreach($param as $key=>$val){
//we have to urlencode the values
$request.= $key."=".urlencode($val);
//append the ampersand (&)
//sign after each paramter/value pair
$request.= "&";
}
//remove the final ampersand sign from the request
$request = substr($request, 0, strlen($request)-1);
//this is the url of the gateway's interface
$url = "http://www.tm4b.com/client/api/send.php";
//initialize curl handle
$ch = curl_init($url);
//set the url
curl_setopt($ch, CURLOPT_URL, $url);
//return as a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
//set POST method
curl_setopt($ch, CURLOPT_POST, 1);
//set the POST variables
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//run the whole process and return the response
$response = curl_exec($ch);
curl_close($ch); //close the curl handle
//show the result onscreen for debugging
//print $response;
Ref: http://www.sephiroth.it/tutorials/flashPHP/sms/
(in the above code from this link modified for error free result. The bolded parameter in the code was missed unexpectedly).
uSiS Portfolio
Jade Research Centre
ZAK Services
Concrete Services International
Mid West Coating
Nilgiris Consulting
LuvYourBody
Moplet
Tirupurgate
Reliance Supply Inc.
Dcare Technologies
Frontline 1 Real Estates
Frontline 1 HR Management Services
Surgenture Inc.
Infognana
DJRON Party
US Pest Environmental Services
New South Equipment Mats
SBG Infosys
Altamonte Springs South Seminole Optimist Club
Asia Insurance
Atlantic Guaranty Insurance Co.
Angappa College of Arts and Science
Rajam Textile Marketings
Applying CSS to forms [ Simple Approach ]
http://www.webcredible.co.uk/user-friendly-resources/css/css-forms.shtml
Form Styling - CSS
Setup Apache, PHP and MySQL
At the moment there are four XAMPP versions:
- a version for Linux systems (tested for Ubuntu, SuSE, RedHat, Mandrake and Debian),
- a version for Windows 98, NT, 2000, 2003, XP and Vista,
- a beta version for Solaris SPARC (developed and tested under Solaris 8),and a beta version for MacOS X.
Start downloading XAMPP here
» XAMPP for Windows
» XAMPP from SourceForge.net
AJAX Cross Domain
Check out this URL for the solution: http://www.ajax-cross-domain.com/
Lets discover the Technology
Here we can discuss on the topics listed below:
Web Development
- PHP
- MySQL
- AJAX
- Java Script
- ASP, ASP.net
- SQL Server
- Web Servers
- Domain and TLDs
- DNS
- .NET Technologies
- Java Development
- Mobile Applications
