【发布时间】:2020-12-06 01:31:06
【问题描述】:
我想要一个 switch 语句,其中包含文字大小写和字符串中带有 通配符 的大小写:
switch($category){
case 'A**': $artist= 'Pink Floyd'; break;
case 'B**': $artist= 'Lou Reed'; break;
case 'C01': $artist= 'David Bowie'; break;
case 'C02': $artist= 'Radiohead'; break;
case 'C03': $artist= 'Black Angels'; break;
case 'C04': $artist= 'Glenn Fiddich'; break;
case 'C05': $artist= 'Nicolas Jaar'; break;
case 'D**': $artist= 'Flat Earth Society'; break;
}
当然 * 在这里会按字面意思理解,因为我将它定义为字符串,所以这不起作用,但你知道我想要完成什么:对于 A、B 和 D 情况,数字可以是任何 ( *)。也许使用 preg_match 这是可能的,但这真的让我大吃一惊。我用谷歌搜索过,我确实做到了。
【问题讨论】:
-
使用 if() {} else if () ... {} else {} 和 substr
-
你不能用开关做到这一点。使用关联数组,以正则表达式作为键,将值作为结果,然后使用 preg_match() 在 foreach() 中传递整个内容。
标签: php string switch-statement case wildcard