2012-07-25 70 views
0

在我玩游戏的网站上工作。在这个页面上,用户可以上传描述一件设备的文本块,并希望我可以通过它解析创建一个对象并将其上传到MySQL。下面是一个例子块:PHP中的嵌套开关

Item:   an ethereal bracelet 
Key words: geq grey_man man ethereal bracelet geq_Guxx 
Type:   treasure 
Level:  50 
Extra flags: rot-death no-transfer 
Weight:  2 
Value:  0 
Affects:  spell-resistance by 5. 
Affects:  dexterity by 1. 
Affects:  strength by 1. 
Affects:  intelligence by 1. 
Affects:  wisdom by 1. 
Affects:  hitroll by 5. 
Affects:  constitution by 1. 
Affects:  hp by 50. 
Affects:  damroll by 10. 

这里是在我的设备类中的函数:

public function create_eq_from_text($text) { 
     $text = trim($_POST['item']); 
     $text = explode("\n", $text); 
     foreach($text as $line) { 
      switch($line) { 
       case strstr($line, 'Item:'): 
        $this->name = trim(str_replace('Item:', '', $line)); 
        break; 
      case strstr($line, 'Key words:'): 
       $line = trim(str_replace('Key words:', '', $line)); 
       $this->key_words = $line; 
       break; 
      case strstr($line, 'Type:'): 
       $line = trim(str_replace('Type:', '', $line)); 
       $this->type = $line; 
       break; 
      case strstr($line, 'Level:'): 
       $line = trim(str_replace('Level:', '', $line)); 
       $this->level = intval($line); 
       break; 
      case strstr($line, 'Extra flags:'): 
       $line = trim(str_replace('Extra flags:', '', $line)); 
       $this->extra_flags = $line; 
       break; 
      case strstr($line, 'Weight:'): 
       $line = trim(str_replace('Weight:', '', $line)); 
       $this->weight = intval($line); 
       break; 
      case strstr($line, 'Value:'): 
       $line = trim(str_replace('Value:', '', $line)); 
       $this->value = intval($line); 
       break; 
      case strstr($line, 'physical attacks'): 
       $this->p_ac = get_number($line); 
       break; 
      case strstr($line, 'magic attacks'): 
       $this->m_ac = get_number($line); 
       break; 
      case strstr($line, 'spell-resistance'): 
       $this->saves = get_number($line); 
       break; 
      case strstr($line, 'dexterity by'): 
       $this->dex = get_number($line); 
       break; 
      case strstr($line, 'strength by'): 
       $this->str = get_number($line); 
       break; 
      case strstr($line, 'intelligence by'): 
       $this->intel = get_number($line); 
       break; 
      case strstr($line, 'wisdom by'): 
       $this->wis = get_number($line); 
       break; 
      case strstr($line, 'constitution by'): 
       $this->con = get_number($line); 
       break; 
      case strstr($line, 'damroll by'): 
       $this->dam = get_number($line); 
       break; 
      case strstr($line, 'hitroll by'): 
       $this->hit = get_number($line); 
       break; 
      case strstr($line, 'hp by'): 
       $this->hp = get_number($line); 
       break; 
      case strstr($line, 'mana by'): 
       $this->mana = get_number($line); 
       break; 
      case strstr($line, 'Damage is'): 
       $line = trim(str_replace('Damage is', '', $line)); 
       $line = str_replace('.', '', $line); 
       $this->weapon_damage = $line; 
       break; 
      } 
     } 
} 

这里是窗体代码:

if(isset($_POST['submit']) && !empty($_POST['item'])) { 
    $item = new Equipment; 
    $item->create_eq_from_text($item); 
    print_r($item); 
    } 

出于某种原因,唯一的开关捕捉的是strstr('Item:')。首先,我认为转换中的突破已经脱离了习惯,但我已经排除了这一点。任何人都可以将我指向正确的方向吗?

+0

对于要评估的案例,它必须匹配传入开关的参数。 – 2012-07-25 04:00:22

+0

@Matthew Scragg strstr()将返回false,如果没有匹配,奇怪的是它可以用于设置$ this->名称,但不会触发其他任何事情。 – 2012-07-25 04:03:29

+0

您必须重新构建......用switch语句,您基本上是在说“这里有一个$ line的值,与所有这些case值进行比较,并在匹配的任何地方运行”。自然$ line!= strstr($ line,'Item:') – Mahn 2012-07-25 04:06:04

回答

0

您不能在这种情况下使用switch语句,因为您正在寻找$ line内的子字符串。 Switch语句查找与给定变量的匹配,而不是布尔值。

+0

我很确定你可以匹配一个开关与一个布尔值在php – 2012-07-25 04:09:17

+0

http://codepad.org/dWSVYqsV – 2012-07-25 04:11:52

+0

你误解我的声明,我的意思是交换机(说一个字符串)与变量之间的匹配案例(一个布尔值) – LeleDumbo 2012-07-25 11:28:28