Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何简化preg_match使用的开关盒?_Php - Fatal编程技术网

Php 如何简化preg_match使用的开关盒?

Php 如何简化preg_match使用的开关盒?,php,Php,我有一个字符串格式: $search_cases = $variable1.'*'.$variable2.'*'.$variable3.'*'.$variable4; 其中所有变量可以是字符串“否”或数值(整数)。我想涵盖所有可能的变化。我发现唯一可行的方法是使用开关盒(而不是if..elseif..)。现在我的代码如下所示: switch($search_cases) { case (preg_match("/no.no.no.no/", $search_cases) ? $sear

我有一个字符串格式:

$search_cases = $variable1.'*'.$variable2.'*'.$variable3.'*'.$variable4;
其中所有变量可以是字符串“否”或数值(整数)。我想涵盖所有可能的变化。我发现唯一可行的方法是使用
开关盒
(而不是
if..elseif..
)。现在我的代码如下所示:

switch($search_cases) {
    case (preg_match("/no.no.no.no/", $search_cases) ? $search_cases : !$search_cases): echo 'no filter <br />'; break;
    case (preg_match("/(\d+).no.no.no/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: location<br />'; break;
    case (preg_match("/no.(\d+).no.no/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: event<br />'; break;
    case (preg_match("/no.no.(\d+).no/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: artist<br />'; break;
    case (preg_match("/no.no.no.(\d+)/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: priceinterval<br />'; break;
    case (preg_match("/(\d+).(\d+).no.no/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: location event<br />'; break;
    case (preg_match("/no.(\d+).(\d+).no/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: event artist<br />'; break;
    case (preg_match("/no.no.(\d+).(\d+)/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: artist priceinterval<br />'; break;
    case (preg_match("/(\d+).no.no.(\d+)/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: location priceinterval<br />'; break;
    case (preg_match("/(\d+).(\d+).(\d+).no/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: location event artist<br />'; break;
    case (preg_match("/no.(\d+).(\d+).(\d+)/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: event artist priceinterval<br />'; break;
    case (preg_match("/(\d+).no.(\d+).(\d+)/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: location artist priceinterval<br />'; break;
    case (preg_match("/(\d+).(\d+).no.(\d+)/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: location event priceinterval<br />'; break;
    case (preg_match("/(\d+).(\d+).(\d+).(\d+)/", $search_cases) ? $search_cases : !$search_cases): echo 'filters used: location event artist priceinterval<br />'; break;
}
开关($search\u案例){
案例(preg_match(“/no.no.no/”,$search_cases)?$search_cases:!$search_cases):回显“no filter
”;中断; 案例(preg_match(“/(\d+).no.no.no/”,$search_cases);$search_cases:!$search_cases):使用的回显“过滤器:位置
”;中断; 案例(preg_match(“/no.(\d+).no.no/”,$search_cases);$search_cases:!$search_cases):使用的回显“过滤器:事件
”;中断; 案例(preg_match(“/no.no.(\d+).no/”,$search_cases)?$search_cases:!$search_cases):echo'使用的过滤器:美工
;中断; 案例(preg_match(“/no.no.no.(\d+)/”,$search_cases)?$search_cases:!$search_cases):使用的回音“过滤器:priceinterval
”;中断; 案例(preg_match(“/(\d+)(\d+)。no.no/”,$search_cases)?$search_cases:!$search_cases):回显“使用的过滤器:位置事件
”;中断; 案例(preg_match(“/no.(\d+)(\d+).no/”,$search_cases);$search_cases:!$search_cases):回显“使用的过滤器:事件艺术家
”;中断; 案例(preg_match(“/no.no.(\d+)(\d+)/”,$search_cases)?$search_cases:!$search_cases):回显“使用的过滤器:艺术家价格区间
”;中断; 案例(预匹配(“/(\d+)。编号(\d+)/”,$search\u案例)?$search\u案例:!$search\u案例):回显“使用的过滤器:位置价格区间
”;中断; 案例(preg_match(“/(\d+)(\d+)(\d+))(\d+).no/”,$search_案例)?$search_案例:!$search_案例):回显“使用的过滤器:位置事件艺术家
”;中断; 案例(preg_match(“/no.(\d+)(\d+)(\d+)/”,$search_cases)?$search_cases:!$search_cases):回显“使用的过滤器:事件艺术家价格区间
”;中断; 案例(预匹配(“/(\d+)。编号(\d+)(\d+)/”,$search\u案例);$search\u案例:!$search\u案例):回显“使用的过滤器:位置艺术家价格区间
”;中断; 案例(预匹配(“/(\d+)(\d+))。编号(\d+/”,$search\u案例)?$search\u案例:!$search\u案例):回显“使用的过滤器:位置事件价格区间
”;中断; 案例(preg_match(“/(\d+)(\d+)(\d+)(\d+)/”,$search_案例);$search_案例:!$search_案例):回显“使用的过滤器:位置事件艺术家价格区间
”;中断; }
当然,我需要在不同的表中执行不同的查询,而不是回显过滤器

基本上,我尝试用它来完成的是,我在页面上有4个过滤器(下拉菜单-彼此独立),我尝试根据所选的过滤器获得结果。信息分散在几个关系表上

所以我的问题是:是否有一种更简单的方法来涵盖不同的情况,或者有其他最佳实践来实现相同的结果?谢谢

编辑:

我需要从3个表中提取数据:

表:事件

表:艺术家

表:价格区间

表:票价

还有其他一些表,如位置(根据事件表中的
location\u id
提取名称),票证类型和票证价格(也都与事件表相关),但与我猜的问题无关

过滤器是:位置/事件/艺术家/价格区间

我需要的结果基于这些,因此如果我选择一个位置(保持其他过滤器不变-VariableNono case),页面将返回该位置的所有事件,然后页面会显示所有活动的地点和艺术家马赫。希望现在更清楚

Edit2


更改了回音以反映不同的情况。对于每个情况,我需要不同的结果。

这当然是可以解决的,不需要任何正则表达式的参与,您只需将四个过滤器作为单独的输入变量而不是这个怪物提交,然后为每个过滤器构建所需的查询/条件

我并没有实际使用DB测试这段代码


-选择位置-
地点1
....
…其他过滤器。。

//处理搜索的PHP代码
$query='从事件ev中选择ev.*';
$joins=array();
$conditions=array();
$params=array();
如果(!空($\u POST['filters']['location'])){
$location=$_POST['filters']['location'];
$conditions[]=“ev.location\u id=:location\u id';
$params[':location_id']=$location;
}
如果(!空($_POST['filters']['artist'])){
$artist=$_POST['filters']['artist'];
$joins[]=“在a.event_id=ev.id上内部加入艺术家a”;
$conditions[]='a.id=:artist_id';
$params[':艺术家id']=$artist;
}
//既然您还没有解释“事件”过滤器的作用,我将进行一个粗略的猜测——如果需要的话进行调整
如果(!empty($_POST['filters']['event'])){
$event=$_POST['filters']['event'];
$conditions[]=“ev.category_id=:cat_id或ev.sub_category_id=:sub_cat_id';
$params[':cat_id']=$event;
$params[':sub_cat_id']=$event;
}
//我并不完全清楚这个过滤器中涉及的表是如何相互连接的——如果需要,请进行调整
如果(!空($_POST['filters']['price'])){
$price=$_POST['filters']['price'];
//以某种方式加载价格间隔
$price\u interval=getPriceInterval($price);
$joins[]='内部联接(从ticket_price tp中选择不同的事件_id,其中tp.price介于:minPrice和:maxPrice之间)p ON p.event_id=ev.id';
$conditions[':minPrice']=$price_interval['minval'];
$conditions[':maxPrice']=$price_interval['maxval'];
}
如果(计数($joins)){
$query.=''.intlode(''$joins);
}
如果(计算($条件)){
foreach($ix=>$condition的条件){
$conditions[$ix]='(“.$condition.”);
}
$qu