note 44825 deleted from function.preg-match by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 18:16:39 +0000 |
| Subject: | note 44825 deleted from function.preg-match by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127582@lists.php.net to get a copy of this message | ||
Note Submitter: rchoudhury --} pinkgreetings {-- com
----
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.