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 输出区间内所有的完数的个数。 要简单点的程序
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 串口发送数据和接收数据
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码