【问题标题】:php switch statement with wildcard in the stringphp switch 语句在字符串中带有通配符
【发布时间】: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


【解决方案1】:

当然,只有当它真的是最好的方法时,你才能使用 switch 来做到这一点。很长的switch案例列表让人头疼……

switch($category){
    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;
    default:
        switch(substr($category,0,1)){
            case A: $artist = 'Pink Floyd';         break;
            case B: $artist = 'Lou Reed';           break;
            case D: $artist = 'Flat Earth Society'; break;
            default:    echo'somethig is wrong with category!';}}

【讨论】:

  • 这正是我要找的地方!不需要 preg_match 让我很开心。非常感谢这个简单的答案。
【解决方案2】:

我写了一个函数。这是preg_match,但它很短且可重复使用。

function preg_switch(string $str, array $rules) {
    foreach($rules as $key => $value) {
        if(preg_match("/(^$key$)/", $str) > 0)
            return $value;
    }
    return null;
}

你可以这样使用它:

$artist = preg_switch("Bdd", [
    "A.." => "Pink Floyd",
    "B.." => "Lou Reed",
    "C01" => "David Bowie",
    "C02" => "Radiohead",
    "C03" => "Black Angels",
    "C04" => "Glenn Fiddich",
    "C05" => "Nicolas Jaar",
    "D.." => "Flat Earth Society",
]);

你必须使用.而不是*

【讨论】:

  • 您为每个键运行 preg_match 并且依赖于不完整的正则表达式
  • 是的。字符串比较更快,但我认为在这种情况下没有太大区别。如果所有字母的数量少于 80 个字符,则执行 1000000 次。时差将小于 1 秒。
  • 不完整的正则表达式是什么意思。你的意思是它在某些情况下不起作用?
  • 我的意思是在你的数组键中,你只有最终正则表达式的一部分,这可能会导致错误。并且密钥本身不会让您知道它可以用作正则表达式。
【解决方案3】:

试试这个:

$rules = [
    '#A(.{2,2})#' => 'Pink Floyd',
    '#B(.{2,2})#' => 'Lou Reed',
    'C01' => 'David Bowie',
    'C02' => 'Radiohead',
    'C03' => 'Black Angels',
    'C04' => 'Glenn Fiddich',
    'C05' => 'Nicolas Jaar',
    '#D(.{2,2})#' => 'Flat Earth Society'
];

$category = 'Dxx';
$out = '';

foreach ( $rules as $key => $value )
{
    /* special case */
    if ( $key[0] === '#' )
    {
        if ( !preg_match($key, $category) )
            continue;

        $out = $value;
        break;
    }
    
    /* Simple key */
    if ( $key === $category )
    {
        $out = $value;
        break;
    }
}

echo $out."\n";

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2011-08-03
    • 1970-01-01
    相关资源
    最近更新 更多