TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-08-2008, 11:36 AM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Big Grin Filter_var or RegEx?

I have been thinking most of the time about Filtering out the Variables or just a Regular Expression for matching different variables like for generally a Membership System with Email.
I want a comparison of Filter_var and RegEx when it comes to matching these vars.

Example of a Filter_Var:
PHP Code:
<?php
$filter1
=(filter_var('bob@example.com'FILTER_VALIDATE_EMAIL));
$filter2=(filter_var('example.com'FILTER_VALIDATE_URLFILTER_FLAG_SCHEME_REQUIRED));

  if(!
$filter) {
     echo 
'This email is not valid!';
} else {
  echo 
'Well pfft.. It is valid -_-';
}
?>
And thus the hard-coded ways:

PHP Code:
            $name "/^[-!#$%&'*+./0-9=?A-Z^_`{|}~]+";
// http://www.(example).com
            
$host "([-0-9A-Z]+.)+";
// This is extension or a TLD( Top Level Domain ) and it involves the Extension of the Host
            
$ext "([0-9A-Z]){2,4}$/i"

Which is more Powerful, and Effective? Obviously Filter_var is simpler in terms of matching vars.
Orc is offline  
Reply With Quote
Old 01-08-2008, 11:50 AM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

There's no technical reason to my choice, but for sheer simplicity I would say to use filters over regex. I don't see the point in writing regex when you can easily use a pre-made filter. I'd only use regex if a filter is inadequate or doesn't exist.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
The Following User Says Thank You to Karl For This Useful Post:
Orc (01-08-2008)
Old 01-08-2008, 11:53 AM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Ah, I thought someone would say something like that! Thanks! :D
Orc is offline  
Reply With Quote
Old 01-08-2008, 12:50 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Another valid point is that people try and tackle regular expressions when they're not fully learned in the subject. I admit, I used to be one of those and I do surmise my older applications are susceptible to regular expression issues.

The very fact that the Filter Vars is an established PHP library would make it beneficial to use above anything else because it will have been thoroughly tested against many various tricks that users may well decide to try.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Orc (01-08-2008)
Old 01-08-2008, 03:04 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

FILTER_VALIDATE_EMAIL only uses a (PCRE) regular expression to validate the email address anyway: it's important to know what the filter will and will not allow as it may possibly be different from what you're prepared to accept in your application. That said, it's not a part of the PHP core for nothing. The regular expression is the same as used in the PEAR QuickForm Email validation.

FILTER_VALIDATE_URL does a few things, and there are flags to further hone the behaviour of the filter. Firstly, this filter removes any invalid characters (as per the appropriate RFC specs) and if nothing is left (ie, the entire string contained invalid characters!) the validation fails. Next, the string is passed through parse_url. If the string cannot be parsed as an url (including any required parts defined by the flags used, if any), the validation fails. If all goes well, the validation is successful.
Note: A scheme is required (http, mailto, file, etc.). Important, an 'url' such as file://c:/myfile.exe or even (a made up one!) talkphp://salathe/pm will be allowed by this filter.
Summary
The above might come across to the reader as negative towards the filters. I like the filters as another tool available for our use, but to be able to effectively use any tool you really have to know how it works which the post above aimed to inform you about.

With all that said, Wildhoney's latest point about people using regular expressions echoes the idea behind this post. The tools are there to be used, whether you understand how/why/when to use the tool is another matter altogether.
Salathe is offline  
Reply With Quote
The Following 2 Users Say Thank You to Salathe For This Useful Post:
Orc (01-08-2008), RobertK (01-08-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:32 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design