PHPBuilder - 10 Easy Examples for Deciphering PHP Regular Expressions Page 2



RSS Twitter
Articles Php Functions

10 Easy Examples for Deciphering PHP Regular Expressions - Page 2

by: W. Jason Gilmore
|
July 20, 2010

6. Finding More Complex Patterns

As you've just learned, locating numbers, numbers consisting of a specified digit count, and even numbers in context is pretty easy. But what if you wanted to locate a more complex string, such as a U.S. social security number? Social security numbers follow a rigid pattern, consisting of three digits, a hyphen, two digits, another hyphen, and finally four more digits (e.g. 123-45-6789). You can locate strings such as this by combining many of the concepts we've discussed so far:
$string = "John is 28 and his social security number is 123-45-6789."; preg_match_all("/b[0-9]{3}-[0-9]{2}-[0-9]{4}b/", $string, $digits);

7. Accounting for Inconsistency

Suppose that for some reason certain documents formatted social security numbers using asterisks instead of hyphens, meaning they could potentially appear in the string looking like 123*45*6789, and even 123-45*6789. You can write a regular expression that can account for the inconsistency, thereby retrieving substrings consisting of all such variations:
$string = "123-45-6789 and 123*45-6789"; preg_match_all("/b[0-9]{3}[-|*][0-9]{2}[-|*][0-9]{4}b/", $string, $digits);

8. Correcting Errors

When searching for strings such as social security numbers, you probably should correct any data-entry errors you find along the way before using the data for other purposes. You can do so as follows:
$string = "John is 28 and his social security number is 123*45-6789."; echo preg_replace('/([0-9]{3})[-*]([0-9]{2})[-*]([0-9]{4})/', '1-2-3', $string);
Executing this example will output the following corrected string:
John is 28 and his social security number is 123-45-6789.

9. Taking Advantage of Alternative Syntax

The [0-9] character class is just one way to represent digits ranging between zero and nine. You'll also regularly encounter several alternative approaches that accomplish the same task yet use different syntax. For instance, all of the following examples will recognize a proper social security number:
/b[0-9]{3}-[0-9]{2}-[0-9]{4}b/ /bd{3}-d{2}-d{4}b/ /b[:digit:]{3}-[:digit:]{2}-[:digit:]{4}b/

10. Avoiding Regular Expressions

Because the need to parse strings and documents for certain substrings is so common, the PHP developers have put a lot of thought into how to streamline the process for certain tasks. For instance, if you merely want to determine whether a specific substring is located in a larger string, use the stristr() function rather than construct a regular expression. Or if you want to validate a complex string such as an email address, check out the Filter extension. By taking advantage of these conveniences you'll save a fair amount of time and frustration, not to mention boost your application's performance.

Conclusion

With some practice, constructing even complex regular expressions will become as natural as riding a bicycle. Spend some time devising your own examples.

About the Author

Jason Gilmore is the founder of WJGilmore.com and the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Third Edition".

« Previous Page
1
|
2

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
W. Jason Gilmore

Comment:



Comment:

(Maximum characters: 1200). You have characters left.