I don’t think I talk about, let alone make use of, Regular Expressions enough. No, I’m not talking about the fact that I don’t smile as much as some people, or tear up at the death of any living object. No, I’m talking about those sometimes difficult to read “formulas” that we computer geeks like to use to figure out if it matches something. To learn the basics of what these suckers are, check out the wiki link.
The sheer power of these things often goes unnoticed until you start to think about what your other functions are doing for you. For example, a function like substr could be implemented using a regular expression. Or how about strpos? That could be done with a regular expression and a string (array) length command. In fact, the more I think about it, a good number of the functions that I use every day could be done better with a preg than the function does it themselves. Why’s that you might ask? Take for example the input of variables.
Often times I have to parse a variable in one way shape or form. By that I mean, often times I have to take in user data, and then convert it into something the computer can store. Most of the time I use some cheap function in php to help me get the job done. The problem with this, is that if I don’t pay attention to what I’m doing, I can often find myself with a security issue because I didn’t realize that a ‘ would break my string (or something of that nature).
With regular expressions though, you can have it match exactly what your looking for. Meaning, only what you want is what you accept for user data. I’ve always done this in the past to validate email addresses, and just recent found myself writing a ton of regular expressions for Ann Sieg Consulting. In any case, if you don’t know regular expression syntax, I suggest to take the time to learn it… You’ll be glad you did.
Comments on this entry are closed.
The most useful thing I ever learned about regular expressions is the idea that they are "greedy" and they'll keep searching for the end of a match … producing unexpected results when you're doing preg_replace's for [[X]] pattern matching.
That one took me a while. 🙂