Validate A String With PHP
Thursday September 23rd 2004
I realize that this post is grossly mis-filed, but since it's late and I'm exhausted and just plain lazy, for now this is where it must reside. This is my first snippet of code I have to offer up to the Karma Gods of "you've been reading people's free tutorials and scripts for years, and it's about damn time you started writing some yourself." Of course there have been plenty of ideas in my head over the years of things I could write, but it was only the other night when true Inspiration struck.
I was actually shopping online for climbing gear (yum), and, well, because I can, I decided to check out the source code of the site I was shopping on. And lo and behond, their entire suite of JavaScript code used by the site was sitting there in plain view with the HTML. Now yes, of course I can follow any old <script> link I find to get at the external js files, but there was something particularly voyeuristic about seeing it defined locally. And so I read. . .
The first function I came upon was ValidateCCard(), or "Validate Credit Card," which was quite intriguing since I had written a js-based credit card validation script for work many moons back. So I copy-and-pasted it into my favorite little editor, and began my "review." Early on in the code I found a call to ValidateString(), to which I subsequently copy-and-pasted as well.
And that's about as far as I got, because the code didn't impress me. In fact, it did quite the opposite, it inspired me to do Better, but with PHP (server-side also means it's accessible). And so I did.
- function string_validate($string_to_validate,$valid_string)
- {
- for ($i = 0; $i < strlen($string_to_validate); $i++)
- {
- if (strpos($valid_string,$string_to_validate{$i}) === false) return false;
- }
- return true;
- }
Pass it two variables, the first being the string to validate, the second being the string of "valid" characters, and the function will return a boolean value of either true or false. It's a fairly straightforward function, with only one real shorthand "trick" (which I learned while writing the script). Line by line, the function does the following:
- define the function, and the two parameters to be passed, $string_to_validate and $valid_string
- I like curly brackets to be on their own line, for readability
- Setting $i to 0 at first and incrementing by 1, and looping while $i is less than the length of $string_to_validate, we will check the following 3 lines (2 of which are curly, so really only one!).
- curly
- if the character found at position $i of $string_to_validate (shorthand trick) is NOT found within $valid_string, return false
- curly
- if we've made it this far then we've successfully checked to make sure that each character in $string_to_validate was not found in $valid_string, so return true!
- one more curly
Make sense? . . . Ok, maybe I didn't explain that too thoroughly. Useful? Perhaps to a point, but PHP might even have a built-in script for this. But didn't I mention that I was tired and lazy? Sorry children, that's all for tonight. . .