dourao3960 2014-10-11 00:13
浏览 19
已采纳

preg_match无法正常工作?

I have recently updated one of my php files to allow for a new preg_match but it seems to work only in the first two instances

//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";
        } 

The issue im having is that my 1st two button's will display fine but when my $ruavalue = 3 then my 3rd button will not work i dont know if im just being blind to the issue or if its particularly bad code

  • 写回答

1条回答 默认 最新

  • dongzhiyong8577 2014-10-11 01:03
    关注

    I think I like leaving those array values as strings, even though they're integers, just because of how my notepad++ colors them with syntax highlighting (text is grey, numbers are red, I like grey). You could do it either way, though.

    <?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>
       ";
    }
    ?>
    

    wow....

    k, so... i dunno. i tested all regex and good/bad values for it all (both sections of code work). not sure why you have those input fields hidden but that's alright.

    <?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';
    }  
    
    ?>
    

    and if you want to keep it your way... this also functions:

    <?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';
    }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗