Page 1 of 1
PHP Class for Regex Functions
Posted: Fri Jun 23, 2006 9:24 am
by Jixxor
I decided to post this in theory as it is just that, a theory to develop a class for common regex functions used constantly. I.E. email validation, city/zip validation, username validation and so on.
I'm wondering if it would just be pointless to build a class to do these functions? The way I see it, classes always simplify what you need to do. You write one piece of code (a function) and you can use that code over and over and over in any arena you need it in (granted it's still within your PHP code

).
What do you guys think?
Re: PHP Class for Regex Functions
Posted: Fri Jun 23, 2006 9:49 am
by Oren
Jixxor wrote:I'm wondering if it would just be pointless to build a class to do these functions?
That's what I thought at first, but after a seconf thought... It's not pointless at all.
Consider this:
You have a large-scaled application and you have some regex which validates emails. Now, you use the same regex over and over again. One day you decide that you could write this regex better. Now what would you do? Would you go over each file in your application and fix it manually?
I don't think so.
Posted: Fri Jun 23, 2006 9:53 am
by Jixxor
That's my logic in this theory. Plus the advantage of being able to reuse the class over and over in other projects I may have down the road.
Right now I'm stuck on designing the class before I get into coding it (it'll be my first time coding even a regex function, classes I've done plenty of before

). So I'm looking for a little guidance in that area.
Posted: Fri Jun 23, 2006 9:53 am
by bmcewan
Hi,
I have started to develop a class to do this very thing, basically allow sanitization of form input. I don't use OOP extensively and am only slowly adapting my direction and feeling my way into it.
The code is only a work in progress, albeit slow. It may benefit from some constructive critique or pointing in the right direction. I have posted this as i felt it relevant to the thread and felt it could help promote further discussion on the subject.
The Class
Code: Select all
<?php
/*
Input Sanitization Class
*/
class cleaner {
var $regex;
var $clean;
var $dirty;
var $error;
function cleaner() {
//empty funtion
}
function set_clean($type = "alpha", $custom = ""){
switch ($type) {
case "alpha":
$this->set_alpha();
break;
case "text":
$this->set_text();
break;
case "numeric":
$this->set_numeric();
break;
case "phone":
$this->set_phone();
break;
case "custom":
$this->set_custom($custom);
break;
default:
break;
}
}
function set_text () {
$this->regex_pos = "#[-a-z,@\.\s&_\"']+#i";
$this->regex_neg = "#[^-a-z,@\.\s&_\"']+#i";
}
function set_numeric () {
$this->regex_pos = "#[\d]+#i";
$this->regex_neg = "#[^\d]+#i";
}
function set_phone () {
}
function set_alpha () {
$this->regex_pos = "#[-a-z0-9 &]+#i";
$this->regex_neg = "#[^-a-z0-9 &]+#i";
}
function set_date () {
// $this->regex = "#^([0-3][0-9])/([0-1][0-9])/([1-2][0-9][0-9][0-9])$#";
}
function set_email () {
$this->regex = "#^([-a-z0-9]+[\._]?){1,}[-a-z0-9]+@(([-a-z0-9]+[_]?){1,})[\.](([a-z]{2,7})[\.]?([a-z]?){2,5})$#i";
}
function set_uri () {
$this->regex = "#^((www.)?[-a-z0-9]+[\._]?)(([-a-z0-9]+[_]?){1,})[\.](([a-z]{2,7})[\.]?([a-z]?){2,5})([-0-9a-z/\._]*)$#i";
}
function set_post () {
$this->regex = "#^(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})$#";
}
function set_currency () {
$this->regex = "#^[0-9]+(.[0-9]{2})?$#";
}
function set_custom ($custom) {
$this->regex = $custom;
}
/**
* @param user_input $input
* @param max_allowed_chars $max
* @param min_allowed_chars $min
*/
function length($input, $max = 0, $min = 0) {
$this->error = strlen($input) < $min ? " must be more than $min characters." : ($max > 0 ? (strlen($input) > $max ? " must no more than $max characters." : false) : false);
return $this->error;
}
function email($input) {
$this->set_email();
$this->error = !preg_match($this->regex, $input) ? "Your email address is invalid." : false;
return $this->error;
}
function currency($input) {
$this->set_currency();
$this->error = !preg_match($this->regex, $input) ? "Amount should be numbers on up to 2 decimal places." : false;
return $this->error;
}
function uri($input) {
$this->set_uri();
$this->error = !preg_match($this->regex, $input) ? "The URL you entered is invalid." : false;
return $this->error;
}
function post_code($input) {
$this->set_post();
$this->error = !preg_match($this->regex, $input) ? "The Postcode you entered is invalid." : false;
return $this->error;
}
/**
* @param regex_type $type *text*numeric*alpha*phone*date*custom
* @param custom_regex $custom e.g #[-a-z0-9 &]+#i
*/
function cleanme ($input, $type = "alpha", $custom = "") {
$this->set_clean($type, $custom);
$this->dirty = preg_replace($this->regex_pos, '', $input);
$this->clean = preg_replace($this->regex_neg, '', $input);
return $this->dirty;
}
}
?>
And the class in use.
Code: Select all
<?php
$clean = new cleaner();
if ($clean->cleanme($advert_userfirst, 'text') != "") {
$errors[] = "Your First name can only include letters. We found ".$clean->dirty. ". They have been removed."; // used for error reporting to the user
$advert_userfirst = $clean->clean; // sets variable to cleaned version ready for db injection
}
if ($clean->cleanme($advert_userlast, 'text') != "") {
$errors[] = "Your First name can only include letters. We found ".$clean->dirty. ". They have been removed."; // used for error reporting to the user
$advert_userlast = $clean->clean; // sets variable to cleaned version ready for db injection
}
$emailcheck = strlen($advert_email) > 0 ? true : false;
$telcheck = strlen($advert_usertelephone) > 0 ? true : false;
if ($emailcheck) {
if ($clean->email($advert_email)) {
$errors[] = $clean->error;
}
if (!unique_email($advert_email)) {
$errors[] = "The email address you supplied is already in use, please try another.";
}
}
?>
Posted: Fri Jun 23, 2006 10:02 am
by Jixxor
Hi,
I have started to develop a class to do this very thing, basically allow sanitization of form input. I don't use OOP extensively and am only slowly adapting my direction and feeling my way into it.
The code is only a work in progress, albeit slow. It may benefit from some constructive critique or pointing in the right direction. I have posted this as i felt it relevant to the thread and felt it could help promote further discussion on the subject.
The Class
...
Looking over your function some more I see it does pretty much what I'm planning on doing. Thanks for the resource, I'll definately be using it for some direction.
Posted: Fri Jun 23, 2006 10:58 am
by Maugrim_The_Reaper
I've only glanced at it - but there is also a PEAR::Validate library of classes which might be a useful reference.
Posted: Fri Jun 23, 2006 11:05 am
by bmcewan
Looking over your function some more I see it does pretty much what I'm planning on doing. Thanks for the resource, I'll definately be using it for some direction.
Thanks, i'll be interested to hear how you proceed.
Re: PHP Class for Regex Functions
Posted: Fri Jun 23, 2006 11:14 am
by Robert Plank
Jixxor wrote:I'm wondering if it would just be pointless to build a class to do these functions? The way I see it, classes always simplify what you need to do. You write one piece of code (a function) and you can use that code over and over and over in any arena you need it in (granted it's still within your PHP code

).
I think you should only write these functions as you need them. Create a static Validate class and call it version 1.0, every time you add to it change it to 1.01 or 1.1 and so on. Otherwise you will be spending a lot of time thinking up a bunch of stuff not getting any work done. Then by the time you actually have to use it your Validate class might have 55 different functions, only 2 of which are actually useful. I've seen tons of validation classes in places like PHPclasses.org so there's no point in doing it just for the sake of doing it.
Posted: Fri Jun 23, 2006 12:47 pm
by alex.barylski
I would personally say this is a bad example of OOP usage, especially in PHP...
The primary idea behind classes and particularly *objects* is to assist in problem solving by allowing the programmer to model real world objects...
Throwing a bunch of quasi-related, but arbitrary functions inside a class...I really hope at the very minimum you are statically invoking these functions and not actually creating an object???
The problem I have with doing anything like this, is that PHP is then required to parse the entire class, which in theory could grow to rediculous sizes...as there are almost an unlimited number of fields one could potentially validate...
The other problem, is that what your really doing is basically wrapping the collection of validation functions in a namespace, which PHP doesn't yet support (to my knowledge anwyays) but using classes as an adhoc hack.
Anyways, you are free to do as you please...I personally don't like it for the reasons mentioned above...
Cheers

Posted: Fri Jun 23, 2006 2:30 pm
by Christopher
I think this problem could be much more cleanly solved with a library of functions. This is not really OO code. The functions would be very small and efficient, like:
Code: Select all
function check_email($email, $error_msg) {
if (preg_match("#^([-a-z0-9]+[\._]?){1,}[-a-z0-9]+@(([-a-z0-9]+[_]?){1,})[\.](([a-z]{2,7})[\.]?([a-z]?){2,5})$#i", $email)) {
return '';
} else {
return $error_msg;
}
}
That seems clearer and considerably less convoluted that the class above. Plus it allows you to provide your own error messages rather than using defined ones.
An OO solution to this problem is to use FilterChain/Filter and Validator/Rule classes.
Posted: Fri Jun 23, 2006 3:32 pm
by Oren
Hockey wrote:I would personally say this is a bad example of OOP usage, especially in PHP...
The primary idea behind classes and particularly *objects* is to assist in problem solving by allowing the programmer to model real world objects...
Throwing a bunch of quasi-related, but arbitrary functions inside a class...I really hope at the very minimum you are statically invoking these functions and not actually creating an object???
The problem I have with doing anything like this, is that PHP is then required to parse the entire class, which in theory could grow to rediculous sizes...as there are almost an unlimited number of fields one could potentially validate...
The other problem, is that what your really doing is basically wrapping the collection of validation functions in a namespace, which PHP doesn't yet support (to my knowledge anwyays) but using classes as an adhoc hack.
Anyways, you are free to do as you please...I personally don't like it for the reasons mentioned above...
Cheers

Yes, but you cannot autoload functions

Posted: Fri Jun 23, 2006 4:04 pm
by Christopher
Oren wrote:Yes, but you cannot autoload functions

How could you? They are not instantiated and
autoload is an error handler for
new. If you wanted create a new_function() it could do the same thing however.
Posted: Sat Jun 24, 2006 3:19 am
by Oren
arborint wrote:How could you?
I know I can't, that's why the:
