[PHP] filter function vs regular expression

2008-12-20 Thread Alain Roger
Hi,

i'm reading a book about PHP and i was wondering why regular expressions are
so often used to check format of variables or emails while the function
filter exists since version 5.2.
What are the plus of regular expression while checking variable format ?
thx.

-- 
Alain
---
Windows XP x64 SP2
PostgreSQL 8.3.5 / MS SQL server 2005
Apache 2.2.10
PHP 5.2.6
C# 2005-2008


Re: [PHP] filter function vs regular expression

2008-12-20 Thread Richard Heyes
 i'm reading a book about PHP and i was wondering why regular expressions are
 so often used to check format of variables or emails while the function
 filter exists since version 5.2.

That's not so long.

 What are the plus of regular expression while checking variable format ?

They' more versatile. Far more in the case of PCRE.

-- 
Richard Heyes

HTML5 Graphing for FF, Chrome, Opera and Safari:
http://www.rgraph.org (Updated December 5th)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] filter function vs regular expression

2008-12-20 Thread Nathan Nobbe
On Sat, Dec 20, 2008 at 9:06 AM, Richard Heyes rich...@php.net wrote:

  i'm reading a book about PHP and i was wondering why regular expressions
 are
  so often used to check format of variables or emails while the function
  filter exists since version 5.2.

 That's not so long.

  What are the plus of regular expression while checking variable format ?

 They' more versatile. Far more in the case of PCRE.


to elaborate, in general, the filter extension should be faster than
corresponding preg_* calls from user space.  why?  b/c, they essentially are
compiled calls to pcre (in many cases) for specific cases, such as email.
check out the C for filter_validate_email(), its pretty simple:

void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
{
/* From
http://cvs.php.net/co.php/pear/HTML_QuickForm/QuickForm/Rule/Email.php?r=1.4*/
const char regexp[] =
/^((\\\[^\f\\n\\r\\t\\b]+\\\)|([\\w\\!\\#\\$\\%'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}]+(\\.[\\w\\!\\#\\$\\%'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}]+)*))@((\\[(((25[0-5])|(2[0-4]

pcre   *re = NULL;
pcre_extra *pcre_extra = NULL;
int preg_options = 0;
int ovector[150]; /* Needs to be a multiple of 3 */
int matches;

re = pcre_get_compiled_regex((char *)regexp, pcre_extra, preg_options
TSRMLS_CC);
if (!re) {
RETURN_VALIDATION_FAILED
}
matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0,
0, ovector, 3);

/* 0 means that the vector is too small to hold all the captured
substring offsets */
if (matches  0) {
RETURN_VALIDATION_FAILED
}
}

basically all it does is call pcre_exec() on against some email regex, and
the string you want to search from userspace.  the difference between that
and a call to preg_match() using the same regex and search string is speed.
the other tradeoff, as richard mentioned is flexibility.  since you cant
possibly conjure / write all possible calls to the regex engine, it makes
sense to expose something like the preg_* functions to userspace.  that
being said, id recommend wrapping the filter_* calls in your validation code
when  where possible, which is essentially the mantra of php programming in
general anyway (stick to the native functions as much as possible).

-nathan

ps.
ill probly setup a test later to see if my half-baked theory is even
accurate :O


Re: [PHP] filter function vs regular expression

2008-12-20 Thread Robert Cummings
On Sat, 2008-12-20 at 16:22 +0100, Alain Roger wrote:
 Hi,
 
 i'm reading a book about PHP and i was wondering why regular expressions are
 so often used to check format of variables or emails while the function
 filter exists since version 5.2.
 What are the plus of regular expression while checking variable format ?
 thx.

Because then your app would lose all compatibility with versions below
5.2.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] filter function vs regular expression

2008-12-20 Thread Per Jessen
Nathan Nobbe wrote:

 to elaborate, in general, the filter extension should be faster than
 corresponding preg_* calls from user space.  why?  b/c, they
 essentially are compiled calls to pcre (in many cases) 

There is no or only the tiniest little difference - AFAICT preg_match
and the filter extension both use the same regex caching mechanism.  If
the regex has not been compiled, the first call will compile it,
subsequent calls will use the already compiled regex.


/Per Jessen, Zürich


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] filter function vs regular expression

2008-12-20 Thread Richard Heyes
/* From
 http://cvs.php.net/co.php/pear/HTML_QuickForm/QuickForm/Rule/Email.php?r=1.4
 */

Huh. I was under the impression that it did full verification, as
according to RFC (2)822. Not that that's generally necessary, in fact
it's mostly just outdated crap, (the technical term I believe).

-- 
Richard Heyes

HTML5 Graphing for FF, Chrome, Opera and Safari:
http://www.rgraph.org (Updated December 20th)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] filter function vs regular expression

2008-12-20 Thread Stan Vassilev | FM



Huh. I was under the impression that it did full verification, as
according to RFC (2)822. Not that that's generally necessary, in fact
it's mostly just outdated crap, (the technical term I believe).

--
Richard Heyes


That's the problem with the filter extension. It's a black box and you never 
know what it checks, or how. Often it validates poor input as is the case 
with its URL validation.


Regex means you see what's happening and you have control over it.

I've been using the filter_* validators in some cases, but after inspecting 
the C source code of this extension and finding multiple side effects in the 
filters I used, I dropped it and went back to regex/strlen/ctype_*/etc.


Regards, Stan Vassilev 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php