preg_match无法正常工作?

时间:2014-10-11 00:13:54

标签: php

我最近更新了我的一个php文件以允许新的preg_match,但它似乎只在前两个实例中起作用

//Normal Values Check
if(preg_match("/norm/i", $drop) && $ruavalue === "0" || $ruavalue === "2" || $ruavalue === "4" || $ruavalue === "6")
        {
        echo '<form action="" method="post">';
        echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
        echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
        echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
        echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
        echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
        echo '<input type="submit" name="ruasubmit" value="RUA!" />';
        echo '</form>';
        } 
//Normal Values Achieved Then Inform User
        elseif(preg_match("/norm/i", $drop) && $ruavalue === "1" || $ruavalue === "3" || $ruavalue === "5" || $ruavalue === "7") {
        echo "You Have RUA'ed To This Boss";
        }
//Heroic Values Check
        elseif(preg_match("/hc/i", $drop) && $ruavalue === "0" || $ruavalue === "1" || $ruavalue === "4" || $ruavalue === "5") 
        {
        echo '<form action="" method="post">';
        echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
        echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
        echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
        echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
        echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
        echo '<input type="submit" name="ruasubmit" value="RUA!" />';
        echo '</form>';
        } 
//Heroic Values Achieved Then Inform User
        elseif(preg_match("/hc/i", $drop) && $ruavalue === "2" || $ruavalue === "3" || $ruavalue === "6" || $ruavalue === "7") {
        echo "You Have RUA'ed To This Boss";
        }
//Mythic Values Check
        elseif(preg_match("/myth/i", $drop) && $ruavalue === "0" || $ruavalue === "1" || $ruavalue === "2" || $ruavalue === "3") 
        {
        echo '<form action="" method="post">';
        echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
        echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
        echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
        echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
        echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
        echo '<input type="submit" name="ruasubmit" value="RUA!" />';
        echo '</form>';
        } 
//Mythic Values Achieved Then Inform User
        elseif(preg_match("/myth/i", $drop) && $ruavalue === "4" || $ruavalue === "5" || $ruavalue === "6" || $ruavalue === "7") {
        echo "You Have RUA'ed To This Boss";
        } 

我遇到的问题是我的前两个按钮会显示正常,但当我的$ ruavalue = 3然后我的第三个按钮将无法工作我不知道我是否只是对这个问题视而不见或者它特别糟糕代码

1 个答案:

答案 0 :(得分:1)

我想我喜欢将这些数组值保留为字符串,即使它们是整数,只是因为我的记事本++如何使用语法高亮显示它们(文本为灰色,数字为红色,我喜欢灰色)。不过你可以这样做。

<?php 
$_POST['tier_two'] = 'tier_two';

$drop         = 'myth';
$ruavalue     = '9';
$tier_two     = $_POST['tier_two'];
$maintoonqry3 = 'ebola!'; 
// ----------------------------------------- 

$vals = array('drop'=>$drop,
              'ruavalue'=>$ruavalue,
              'tier_two'=>$tier_two,
              'maintoonqry3'=>$maintoonqry3); 

$test = array('norm'=>array('0','2','4','6'), 
              'hc'  =>array('0','1','4','5'),
              'myth'=>array('0','1','2','3')); 

foreach ($test as $key => $array) {
   if (preg_match("/$key/i", $drop)) {
      if (in_array($ruavalue,$array)) {
         display_my_form($vals);
      }
      else echo 'You Have RUA\'ed To This Boss'; 
      break;  // put this here if you only want to hit
              // the first key (norm,hc,myth) match, 
              // determine valid or not, an d then stop.  
   } 
}   

function display_my_form($vals) {
   echo "
   <form action='' method='post'>
      <input name='drop1'     type='hidden' value='{$vals['drop']}'>
      <input name='ruavalue1' type='hidden' value='{$vals['ruavalue']}'>
      <input name='boss'      type='hidden' value='{$vals['tier_two']}'>
      <input name='main'      type='hidden' value='{$vals['maintoonqry3']}'>
      <input name='the_page'  type='hidden' value='RUA/rua-system.php'>
      <input name='ruasubmit' type='submit' value='RUA!' />
   </form>
   ";
}
?>

...哇

k,所以...我不知道。我测试了所有的正则表达式和好/坏值(两个代码部分工作)。不知道为什么你隐藏了那些输入字段但是没关系。

<?php 
$_POST['tier_two'] = 'tier_two';   // for testing 

$drop         = 'myth'; 
$ruavalue     = '5'; 
$tier_two     = $_POST['tier_two']; 
$maintoonqry3 = 'ebola!';  

$vals = array('drop'=>$drop,'ruavalue'=>$ruavalue,
              'tier_two'=>$tier_two,'maintoonqry3'=>$maintoonqry3);   


if (preg_match('/norm/i', $drop)) {
   if (in_array($ruavalue,array('0','2','4','6'))) {
      display_my_form($vals);
   }
   else echo_error(); 
}

if (preg_match('/hc/i', $drop)) { 
   if (in_array($ruavalue,array('0','1','4','5'))) { 
      display_my_form($vals);
   }
   else echo_error();  
}

if (preg_match('/myth/i', $drop)) { 
   if (in_array($ruavalue,array('0','1','2','3'))) {
      display_my_form($vals);
   }
   else echo_error();  
}

function display_my_form($vals) {?>
   <form action='' method='post'>
      <input name='drop1'     type='hidden' value='<?php echo $vals['drop'] ?>'>
      <input name='ruavalue'  type='hidden' value='<?php echo $vals['ruavalue'] ?>'>
      <input name='boss'      type='hidden' value='<?php echo $vals['tier_two'] ?>'>
      <input name='main'      type='hidden' value='<?php echo $vals['maintoonqry3'] ?>'>
      <input name='the_page'  type='hidden' value='RUA/rua-system.php'>
      <input name='ruasubmit' type='submit' value='RUA!' />
   </form>
<?php  
}  

function echo_error() {
   echo 'You Have RUA\'ed To This Boss';
}  

?>

如果你想按照自己的方式行事......这也有用:

<?php 
$_POST['tier_two'] = 'tier_two'; 


$drop         = 'myth'; 
$ruavalue     = '5'; 
$tier_two     = $_POST['tier_two']; 
$maintoonqry3 = 'ebola!'; 



if (preg_match('/norm/i', $drop) && in_array($ruavalue,array('0','2','4','6'))) { ?>
   <form action='' method='post'>
      <input name="drop1"     type='hidden' value='<?php echo $drop ?>'>
      <input name="ruavalue1" type='hidden' value='<?php echo $ruavalue ?>'>
      <input name="boss"      type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
      <input name="main"      type='hidden' value='<?php echo $maintoonqry3 ?>'>
      <input name='the_page'  type='hidden' value='RUA/rua-system.php'>
      <input name='ruasubmit' type='submit' value='RUA!' />
   </form>
<?php 
} elseif (preg_match("/norm/i", $drop) && in_array($ruavalue,array('1','3','5','7'))) {
   echo "You Have RUA'ed To This Boss";
} elseif (preg_match("/hc/i", $drop) && in_array($ruavalue,array('0','1','4','5'))) { ?>
   <form action="" method="post">
      <input name='drop1'     type='hidden' value='<?php echo $drop ?>'>
      <input name='ruavalue1' type='hidden' value='<?php echo $ruavalue ?>'>
      <input name='boss'      type='hidden' value='<?php echo $_POST['tier_two'] ?>'> 
      <input name='main'      type='hidden' value='<?php echo $maintoonqry3 ?>'>
      <input name='the_page'  type='hidden' value='RUA/rua-system.php'>
      <input name='ruasubmit' type='submit' value='RUA!' />
   </form>
<?php 
} elseif (preg_match("/hc/i", $drop) && in_array($ruavalue,array('2','3','6','7'))) { 
   echo "You Have RUA'ed To This Boss";
} elseif (preg_match("/myth/i", $drop) &&  in_array($ruavalue,array('0','1','2','3'))) { ?>
   <form action='' method='post'>
   <input name='drop1'     type='hidden' value='<?php echo $drop ?>'>
   <input name='ruavalue1' type='hidden' value='<?php echo $ruavalue ?>'>
   <input name='boss'      type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
   <input name='main'      type='hidden' value='<?php echo $maintoonqry3 ?>'>
   <input name='the_page'  type='hidden' value='RUA/rua-system.php'>
   <input name='ruasubmit' type='submit' value='RUA!' />
   </form>
<?php 
} elseif (preg_match('/myth/i', $drop) &&  in_array($ruavalue,array('4','5','6','7'))) {  
   echo 'You Have RUA\'ed To This Boss';
}
?> 
相关问题