Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:4827 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37955 invoked by uid 1010); 14 Oct 2003 15:03:35 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 37863 invoked from network); 14 Oct 2003 15:03:34 -0000 Received: from unknown (HELO moutng.kundenserver.de) (212.227.126.183) by pb1.pair.com with SMTP; 14 Oct 2003 15:03:34 -0000 Received: from [212.227.126.206] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1A9Qhw-0003Hg-00; Tue, 14 Oct 2003 17:03:32 +0200 Received: from [217.160.91.103] (helo=php.net) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1A9Qhw-000187-00; Tue, 14 Oct 2003 17:03:32 +0200 Message-ID: <3F8C1063.7040907@php.net> Date: Tue, 14 Oct 2003 17:04:03 +0200 Reply-To: hartmut@php-groupies.de User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20030925 X-Accept-Language: en, de MIME-Version: 1.0 To: David Sklar CC: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] regex operators From: hartmut@php.net (Hartmut Holzgraefe) David Sklar wrote: > I was thinking about adding one or two regex-related features to the engine: > > 1. "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