Regex help

Discussion in 'PHP' started by andytan91, Aug 6, 2010.

  1. #1
    Hello guys i have troubleshooted this problem for 1hour+.. i dont understand why does it not pass a value of "0" when $_POST['AuditGlobal'] == "2". It prints out "2" instead. I have a few scripts above that are exactly the same as this hence i am confused..hope you guys can help me out..thanks..

    Problem Script
    
    if(isset($_POST['AuditGlobal']) && !empty($_POST['AuditGlobal']) && $_POST['submitFinal'] == "Update" )
    {
        
    if($_POST['AuditGlobal'] == "2")
    {
        $newArgument2 = "0";
    }    
    elseif ($_POST['AuditGlobal'] == "1")
    {
        $newArgument2 = "1";
    }
    $newArgument2 = $_POST['AuditGlobal'];
    $batch_contents = preg_replace('~(findstr /i /C:"MACHINE\\\\System\\\\CurrentControlSet\\\\Control\\\\Lsa\\\\AuditBaseObjects=4,)([0-9])("\sC:\\\\\major_security_settings1.txt)~', '${1}'.$newArgument2.'${3}', $batch_contents);
    file_put_contents($batchfileSP, $batch_contents);
    
    echo '<script type="text/javascript">
    alert("Update Successful");
    location.href="menu.php";
    </script>';
    }
    
    PHP:
    Audit:Audit the Access of Global System Objects:<select name="AuditGlobal">
    <option selected=""></option>
    <option value="1">Enable</option>
    <option value="2">Disable</option>
    </select><p>

    Working Script
    if(isset($_POST['SID']) && !empty($_POST['SID']) && $_POST['submitFinal'] == "Update" )
    {
        
    if($_POST['SID'] == "2")
    {
        $newArgument = "0";
    }    
    elseif ($_POST['SID'] == "1")
    {
        $newArgument = "1";
    }
    
    
    $batch_contents = preg_replace('~(findstr /i /C:"LSAAnonymousNameLookup = )([0-9])~', '${1}'.$newArgument, $batch_contents);
    file_put_contents($batchfileSP, $batch_contents);
    
    
    
    echo '<script type="text/javascript">
    alert("Update Successful");
    location.href="menu.php";
    </script>';
    }
    
    PHP:
    Network Access:Allow Anonymous SID/Name Translation:<select name="SID">
    <option selected=""></option>
    <option value="1">Enable</option>
    <option value="2">Disable</option>
    </select><p>
     
    andytan91, Aug 6, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    I think its because your redefining it on this line after your checks

    $newArgument2 = $_POST['AuditGlobal'];

    So just remove that line and try ?
     
    MyVodaFone, Aug 6, 2010 IP
    andytan91 likes this.
  3. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    yeah it works now!..sorry for the trouble, my eyes are so poor lol..thanks alot dude
     
    andytan91, Aug 6, 2010 IP
  4. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    hello guys i have been trying to regex the line below.. i would like to regex the .* part in the 2nd (.*\) and i still can't figure out how to do it..

    $parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);

    This is the closest i can get to
    $batch_contents = preg_replace('~(preg_split\("/\\\\.\\\\(\.\*\\\\)\\\\.\\\\()([0-9.]+)(\\\\))~i', '${1}'.$newArgument, $batch_contents);
     
    andytan91, Aug 9, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    Can you post what the actually line is and how you want it split into parts ?
     
    MyVodaFone, Aug 9, 2010 IP
  6. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    opps i mean i am trying to preg_replace this line --> $parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);

    The bold characters ".*" is what i am intending to replace with numbers and strings...
     
    andytan91, Aug 9, 2010 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    I am not sure if you need to keep the brackets or not ?

    But here goes:

    
    $part1 ='letters';
    $part2 ='1234567';
    
    $string ='$parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);';
    $new_string = preg_replace("#\/\\\.\\\\\(\.\*\\\\\)#","/\.\\($part1)", $string);
    $new_string = preg_replace("#\\\.\\\\\(\.\*\\\\\)#","\.\\($part2)", $new_string);
    
    echo $new_string;
    
    PHP:
    Will echo out:
    
    $parts = preg_split("/\.\(letters)\.\(1234567)/", $file);
    Original string was:
    $parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);
    
    Code (markup):
     
    MyVodaFone, Aug 10, 2010 IP
  8. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    Yeah i will need to retain every thing in $parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);
    Ahhh, nevermind ima integrate the codes now and see how it goes!
    Thanks for the help man:)
     
    andytan91, Aug 10, 2010 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    Opps sorry, you just add \\ for \ in the replacement.

    
    $string ='$parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);';
    $new_string = preg_replace("#\/\\\.\\\\\(\.\*\\\\\)#","/\.\\($part1\\)", $string);
    $new_string = preg_replace("#\\\.\\\\\(\.\*\\\\\)#","\.\\($part2\\)", $new_string);
    
    PHP:
    Out put:
    $parts = preg_split("/\.\(letters\)\.\(1234567\)/", $file); 
    Code (markup):
     
    MyVodaFone, Aug 10, 2010 IP
  10. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #10
    Hmm i have tried putting the $new_string variable into my script..but it does not work allow my filenames with username SEAN to appear..may i ask is there a way to directly change the preg_split function inside the while loop?

    <?php
    
    
      //"$DOCUMENT_ROOT"."new/"; 
      $current_dir = 'C:\xampp\htdocs\Audit_Reports';   //Put in second part, the directory - without a leading slash but with a trailing slash!
      $dir = opendir($current_dir);        // Open the sucker
     
    $newArgument = $_POST[argument];
    
    
    
      echo ("<p><h1>List of Audit Reports:</h1></p><hr><br />");
      while ($file = readdir($dir))            // while loop
     
     {
    $string ='preg_split("/\.\(.*\)\.\(.*\)/", $file);';
    $part1 ='letters';
    $part2 ='.*';
    $new_string = preg_replace("#\/\\\.\\\\\(\.\*\\\\\)#","/\.\\($newArgument\)",$string);
    $new_string = preg_replace("#\\\.\\\\\(\.\*\\\\\)#","\.\\($part2\)", $new_string);
    //echo $new_string;
    
        // $parts = preg_split("/\.\(SEAN\)\.\(.*\)/", $file);
        if (is_array($new_string) && count($new_string) > 1) {    // does the dissected array have more than one part
            $extension = reset($new_string);        // set to we can see last file extension
            if ($extension == "Audit_Report" OR $extension == "audit_report")    // is extension ext or EXT ?
                 echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />";    // If so, echo it out else do nothing cos it's not what we want
            }
        }
      echo "<hr><br />";
      closedir($dir);        // Close the directory after we are done
    
    
    
    ?>
    
    <form action="readdirectory.php" method='post'>Search Files:<p>
    Hostname: <input type="text" name="argument"<p>
    <p><input type="submit" name="submitFinal" value="Update"> &nbsp;<p>
    </form>
    </p>
    
    PHP:
    This is the line i want to replace...// $parts = preg_split("/\.\(SEAN\)\.\(.*\)/", $file);
     
    andytan91, Aug 10, 2010 IP