I was thinking about adding one or two regex-related features to the engine:
- "preg_case": this would behave just like case but instead of doing an
equality comparison, would match against a regular expression, e.g.
switch($data) {
preg_case '/^\d{5}(-\d{4})?$/':
print "US Postal Code";
break;
preg_case '/^[a-z]\d[a-z] ?\d[a-z]\d$/i';
print "Canadian Postal Code";
break;
default:
print "something else!";
}
Where should any captured subpatterns go?
- A regex match operator that returns an array containing subpatterns. If
there is no match against the regex, then an empty array (or just false?)
would be returned.
if ($data =~ '/^(\d{5})(-\d{4})?$/') {
print "The whole postal code is $data[0].";
print "The first five digits is $data[1].";
if ($data[2]) { print "The ZIP+4 is $data[2].";}
}
Some issues with adding these features:
- It creates an engine dependency on the PCRE library.
- There would have to be some new opcodes and parser tokens
- Ideally the code that implements these operators could share as much as
possible with what's already been done in the PCRE extension -- is that
possible?
Comments?
Thanks,
David
Hi David,
2 weeks ago Hartmut Holzgraefe had similar idea :
switch ($data, "preg_match") {
case '/foo.?bar/i' : / computation here */
break;
}
The second argument is a callback.
Andrey
David Sklar wrote:
I was thinking about adding one or two regex-related features to the engine:
- "preg_case": this would behave just like case but instead of doing an
equality comparison, would match against a regular expression, e.g.switch($data) {
preg_case '/^\d{5}(-\d{4})?$/':
print "US Postal Code";
break;
preg_case '/^[a-z]\d[a-z] ?\d[a-z]\d$/i';
print "Canadian Postal Code";
break;
default:
print "something else!";
}Where should any captured subpatterns go?
- A regex match operator that returns an array containing subpatterns. If
there is no match against the regex, then an empty array (or just false?)
would be returned.if ($data =~ '/^(\d{5})(-\d{4})?$/') {
print "The whole postal code is $data[0].";
print "The first five digits is $data[1].";
if ($data[2]) { print "The ZIP+4 is $data[2].";}
}Some issues with adding these features:
- It creates an engine dependency on the PCRE library.
- There would have to be some new opcodes and parser tokens
- Ideally the code that implements these operators could share as much as
possible with what's already been done in the PCRE extension -- is that
possible?Comments?
Thanks,
David
David Sklar wrote:
I was thinking about adding one or two regex-related features to the engine:
- "preg_case": this would behave just like case but instead of doing an
equality comparison, would match against a regular expression, e.g.switch($data) {
preg_case '/^\d{5}(-\d{4})?$/':
print "US Postal Code";
break;
preg_case '/^[a-z]\d[a-z] ?\d[a-z]\d$/i';
print "Canadian Postal Code";
break;
default:
print "something else!";
}
i've started to play with a more general way to handle this:
switch(mixed data [, callback compare_function])
{
...
"compare_function" would default to the functionality of the
"==" operator to show the current behavior
"compare_function" will be called with two parameters:
the switch expression and the case value and should
return true if both match, false otherwise
as a special case "compare_function" should also accept
"==="
this way we do not have to add new keywords and are not
limited to preg expresions here,
possible uses not possible now would include:
switch($foobar, "===") {
case false: ...
case 0: ...
case '': ...
switch($foobar, "preg_match") {
case '/^\d{5}$/': ...
case '/(foo)?bar/': ...
switch($foobar, "fnmatch") {
case '.gif': ...
case '.jpg': ...
switch($foober, "my_custom_function") {
...
switch($foobar, array($this, "compare_function") {
...
--
Hartmut Holzgraefe <hartmut@php.net
Hartmut Holzgraefe wrote:
David Sklar wrote:
I was thinking about adding one or two regex-related features to the
engine:
- "preg_case": this would behave just like case but instead of doing an
equality comparison, would match against a regular expression, e.g.switch($data) {
preg_case '/^\d{5}(-\d{4})?$/':
print "US Postal Code";
break;
preg_case '/^[a-z]\d[a-z] ?\d[a-z]\d$/i';
print "Canadian Postal Code";
break;
default:
print "something else!";
}i've started to play with a more general way to handle this:
switch(mixed data [, callback compare_function])
seems the sanest thing up until now.
slighly O.T.: would be and even better if lambda functions were
available here
{
..."compare_function" would default to the functionality of the
"==" operator to show the current behavior"compare_function" will be called with two parameters:
the switch expression and the case value and should
return true if both match, false otherwiseas a special case "compare_function" should also accept
"==="this way we do not have to add new keywords and are not
limited to preg expresions here,
possible uses not possible now would include:switch($foobar, "===") {
case false: ...
case 0: ...
case '': ...switch($foobar, "preg_match") {
case '/^\d{5}$/': ...
case '/(foo)?bar/': ...switch($foobar, "fnmatch") {
case '.gif': ...
case '.jpg': ...switch($foober, "my_custom_function") {
...switch($foobar, array($this, "compare_function") {
...
--
NetCat
FREE 10MB email + Antivirus + AntiSpam + POP3 + more....
Get it at http://www.doal.co.il:81/free/?c=both
netcat wrote:
i've started to play with a more general way to handle this:
switch(mixed data [, callback compare_function])
seems the sanest thing up until now.
slighly O.T.: would be and even better if lambda functions were
available here
http://php.net/create-function
--
Hartmut Holzgraefe <hartmut@php.net
Hartmut Holzgraefe wrote:
netcat wrote:
i've started to play with a more general way to handle this:
switch(mixed data [, callback compare_function])
seems the sanest thing up until now.
slighly O.T.: would be and even better if lambda functions were
available here
Thanks for the link.
It's just feels a little awkward to see something like:
switch($x,create_function('$a,$b','return $a=="MY$b;"')) {
}
Again, it's a feeling, maybe it's just me.
It doesn't feel right to me that inside definition of new function
i have to escape quotes (that's because create_function
is a function and not special construction on the other side if
it would be a construct it can't return a value as in
$x=create_function(...) ).
Anyway, i have nothing better to suggest (yet) ....
I hope one of you have some suggestion that will make create_function
more usable.
--
NetCat
SPAM-Free 10mb Free email + Antivirus + POP3 + more...
Get it at http://www.doal.co.il:81/free/?c=all-spam
- Thus wrote David Sklar (sklar@sklar.com):
I was thinking about adding one or two regex-related features to the engine:
- "preg_case": this would behave just like case but instead of doing an
equality comparison, would match against a regular expression, e.g.switch($data) {
preg_case '/^\d{5}(-\d{4})?$/':
print "US Postal Code";
break;
preg_case '/^[a-z]\d[a-z] ?\d[a-z]\d$/i';
print "Canadian Postal Code";
break;
default:
print "something else!";
}
[...]Some issues with adding these features:
- It creates an engine dependency on the PCRE library.
- There would have to be some new opcodes and parser tokens
- Ideally the code that implements these operators could share as much as
possible with what's already been done in the PCRE extension -- is that
possible?
I'm thinking a more viable solution would be to do something like:
mixed preg_match ( mixed pattern, string subject [, array matches [, int flags]])
if pattern is an array, preg_match will cycle through each one till
a match is made; returns the index of the array passed (that
matched) or false if not found.
This should also be BC if you just pass one string.
Curt
"I used to think I was indecisive, but now I'm not so sure."
This should also be BC if you just pass one string.
http://docs.php.net/en/function.preg-grep.html
we already have that too.
Derick
--
"Interpreting what the GPL actually means is a job best left to those
that read the future by examining animal entrails."
Derick Rethans http://derickrethans.nl/
International PHP Magazine http://php-mag.net/
"Curt Zirzow" php-dev@zirzow.dyndns.org wrote in message
news:20031014165039.GP51024@bagend.shire...
- Thus wrote David Sklar (sklar@sklar.com):
I was thinking about adding one or two regex-related features to the
engine:
- "preg_case": this would behave just like case but instead of doing an
equality comparison, would match against a regular expression, e.g.switch($data) {
preg_case '/^\d{5}(-\d{4})?$/':
print "US Postal Code";
break;
preg_case '/^[a-z]\d[a-z] ?\d[a-z]\d$/i';
print "Canadian Postal Code";
break;
default:
print "something else!";
}
[...]Some issues with adding these features:
- It creates an engine dependency on the PCRE library.
- There would have to be some new opcodes and parser tokens
- Ideally the code that implements these operators could share as much
as
possible with what's already been done in the PCRE extension -- is that
possible?I'm thinking a more viable solution would be to do something like:
mixed preg_match ( mixed pattern, string subject [, array matches [, int
flags]])if pattern is an array, preg_match will cycle through each one till
a match is made; returns the index of the array passed (that
matched) or false if not found.
It's the same if you have the array with patterns and you make a simple
foreach() cycle. I don't think that functions with internal loops should be
created, when we have a clear solution without it.
This should also be BC if you just pass one string.
Curt
"I used to think I was indecisive, but now I'm not so sure."