PHP中的切换案例中的Preg_match?

时间:2016-08-08 13:21:18

标签: php switch-statement preg-match case

我在php中创建切换功能,我想知道是否有可能做出这样的情况之一:

+ (UIFont *)addFallbacktoFont:(UIFont*)font{
   UIFontDescriptor* originalDescriptor = [font fontDescriptor];

   UIFontDescriptor* fallbackDescriptor = [originalDescriptor fontDescriptorByAddingAttributes:@{UIFontDescriptorNameAttribute:@"Helvetica Neue"}];

   UIFontDescriptor* repaired = [originalDescriptor fontDescriptorByAddingAttributes:@{UIFontDescriptorCascadeListAttribute:@[
                                                                                            fallbackDescriptor
                                                                                            ]}];

   font = [UIFont fontWithDescriptor:repaired size:0.0];

   return font;
}  

我想执行最后一种情况,例如$ message:“project start”或“start project”。我认为这是因为preg_match返回0或1.有没有办法使这段代码有效?

3 个答案:

答案 0 :(得分:0)

如果您希望保存代码的可读性和编译器友好性,请使用此选项:

switch($message){
        case 'hi':
            $output = $jsonWelcome;
            break;
        default:
            $output = preg_match('/start/', $message) ? $jsonHelp : "";
}

答案 1 :(得分:-1)

您可以使用此代码段:

switch(true){
        case $message == 'hi':
            $output = $jsonWelcome;
            break;
        case 1 == preg_match('/start/', $message):
            $output = $jsonHelp;
            break;
}

所有条件都应评估true

答案 2 :(得分:-1)

let always = (() => {
  let worker = (f, done) => {
    return Promise.resolve(f()).then(done, done);
  };
  return function always(f) {
    return [
      result => worker(f, () => result),
      error  => worker(f, () => { throw error; })
    ];
  }
})();

test("p1", 500, false, "good").then(...always(
  () => {
    test("p2", 500, true, "bad");
  }
));

function test(name, delay, fail, value) {
  // Make our test promise
  let p = new Promise((resolve, reject) => {
    console.log(name, `created with ${delay}ms delay before settling`);
    setTimeout(() => {
      if (fail) {
        console.log(name, "rejecting");
        reject(value);
      } else {
        console.log(name, "resolving");
        resolve(value);
      }
    }, delay);
  });

  // Use it
  return p.then(
    result => {
      console.log(name, "initial resolution:", result);
      return result;
    },
    error => {
      console.log(name, "initial rejection; propagating it");
      throw error;
    }
  )
  .then(...always(() => {
    console.log(name, "in finally");
  }))
  .then(
    result => {
      console.log(name, "resolved:", result);
    },
    error => {
      console.log(name, "rejected:", error);
    }
  );
}

应该可以正常工作。

工作代码示例: http://sandbox.onlinephpfunctions.com/code/99ab518718ce9488277619a33dbfdf353f0c2316