Home arrow PHP arrow Page 4 - Strings: Trimming and Counting

Taking Advantage of PEAR: Validate_US - PHP

In this conclusion to a five-part series on strings and regular expressions in PHP, you'll learn about padding and stripping a string, trimming characters from a string, counting the characters in a string, and more. This article is excerpted from chapter nine of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Strings: Trimming and Counting
  2. Counting Characters and Words
  3. Counting the Total Number of Words in a String
  4. Taking Advantage of PEAR: Validate_US
By: Apress Publishing
Rating: starstarstarstarstar / 1
July 15, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Regardless of whether your Web application is intended for use in banking, medical, IT, retail, or some other industry, chances are that certain data elements will be commonplace. For instance, it’s conceivable you’ll be tasked with inputting and validating a telephone number or a state abbreviation, regardless of whether you’re dealing with a client, a patient, a staff member, or a customer. Such repeatability certainly presents the opportunity to create a library that is capable of handling such matters, regardless of the application. Indeed, because we’re faced with such repeatable tasks, it follows that other programmers are, too. Therefore, it’s always prudent to investigate whether somebody has already done the hard work for you and made a package available via PEAR.


Note  If you’re unfamiliar with PEAR, take some time to review Chapter 11 before continuing.


Sure enough, a quick PEAR search turns upValidate_US, a package that is capable of validating various informational items specific to the United States. Although still in beta at press time,Validate_USwas already capable of syntactically validating phone numbers, SSNs, state abbreviations, and ZIP codes. This section shows you how to install and implement this immensely useful package.

Installing Validate_US

To take advantage of Validate_US, you need to install it. The process for doing so follows:

%>pear install -f Validate_US
WARNING: failed to download pear.php.net/Validate_US within preferred state "stable", will instead download version 0.5.2, stability "beta"
downloading Validate_US-0.5.2.tgz ... Starting to download Validate_US-0.5.2.tgz (6,578 bytes)
.....done: 6,578 bytes
install ok: channel://pear.php.net/Validate_US-0.5.2

Note that becauseValidate_USis a beta release (at the time of this writing), you need to pass the-foption to theinstallcommand in order to force installation.

Using Validate_US

The Validate_US package is extremely easy to use; simply instantiate the Validate_US() class and call the appropriate validation method. In total there are seven methods, four of which are relevant to this discussion:

phoneNumber(): Validates a phone number, returningTRUEon success, andFALSEotherwise. It accepts phone numbers in a variety of formats, includingxxx xxx-xxxx,(xxx) xxx-xxxx, and similar combinations without dashes, parentheses, or spaces. For example,(614)999-9999,6149999999, and(614)9999999are all valid, whereas(6149999999,614-999-9999, and614999are not.

postalCode(): Validates a ZIP code, returningTRUEon success, andFALSEotherwise. It accepts ZIP codes in a variety of formats, includingxxxxx,xxxxxxxxx,xxxxx-xxxx, and similar combinations without the dash. For example,43210and43210-0362are both valid, whereas4321and4321009999are not.

region(): Validates a state abbreviation, returningTRUEon success, andFALSEotherwise. It accepts two-letter state abbreviations as supported by the U.S. Postal Service (http://www.usps.com/ ncsc/lookups/usps_abbreviations.html). For example,OH,CA, andNYare all valid, whereasCC,DUI, andBASF are not.

ssn(): Validates an SSN by not only checking the SSN syntax but also reviewing validation information made available via the Social Security Administration Web site (http://www.ssa.gov/), returningTRUE on success, andFALSEotherwise. It accepts SSNs in a variety of formats, includingxxx-xx-xxxx,xxx xx xxx,xxx/xx/xxxx,xxx\txx\txxxx(\t= tab),xxx\nxx\nxxxx(\n= newline), or any nine-digit combination thereof involving dashes, spaces, forward slashes, tabs, or newline characters. For example,479-35-6432and591467543are valid, whereas999999999,777665555, and45678 are not.

Once you have an understanding of the method definitions, implementation is trivial. For example, suppose you want to validate a phone number. Just include theValidate_USclass and callphoneNumber()like so:

<?php
   
include "Validate/US.php";
   
$validate = new Validate_US();
   
echo $validate->phoneNumber("614-999-9999") ? "Valid!" : "Not valid!";
?>

BecausephoneNumber()returns a Boolean, in this example theValid!message will be returned. Contrast this with supplying614-876530932tophoneNumber(), which will inform the user of an invalid phone number.

Summary

Many of the functions introduced in this chapter will be among the most commonly used within your PHP applications, as they form the crux of the language’s string-manipulation capabilities.

In the next chapter, we examine another set of well-worn functions: those devoted to working with the file and operating system.   



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: