note 44825 added to function.preg-match

From: Date: Tue, 17 Aug 2004 16:57:26 +0000
Subject: note 44825 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-80066@lists.php.net to get a copy of this message
I was looking for an easy way to match multiple conditions inside a switch, and preg_match() seemed like a straightforward solution: <?php // stuff foreach (func_get_arg(0) as $key => $value) { switch ($key) { case preg_match("//^(meta_keywords | meta_desc | doctype | xmlns | lang | dir | charset)$/"): $this->g_page_vars[$key] = $value; break 1; case preg_match("//^(site_title|site_desc|site_css)$/"): $this->g_page_vars[$key] = $g_site_vars[$key]; break 1; } // do stuff inside loop } // etc ?> However, while it seemed to work on one server using php 4.3.8, where it accepted only one argument (pattern) and assumed the second one (subject) to be $key, another server running 4.3.8 breaks and returns an obvious warning of "Warning: preg_match() expects at least 2 parameters, 1 given". You probably think "why not just give preg_match a second argument then?" -- well, if we were to do that it'd be $key in this context, but that returns this error: "Warning: Unknown modifier '^'". So now the regex is bad? One possible solution may lie in php.ini settings, though since I don't have access to that file on either server I can't check and find out. http://www.phpbuilder.com/lists/php-developer-list/2003101/0201.php has some comments and other suggestions for the same concept, namely in using: <?php switch(true) { case preg_match("/regex/",$data): // etc. } ?> ...but this doesn't address the current single argument problem. Either way, it's a useful way of working a switch, but it might not work. ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44825 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44825&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44825&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80066) next »