Printing out a specific string in a file

Discussion in 'PHP' started by andytan91, Jul 20, 2010.

  1. #1
    Hello guys im a newbie in php.. basically what i want to do it to print out the a certain string in a line. In this case, i want to retrieve out text that is after ":" and before policies.txt in the lines below for eg. Success or Failure.

    I think i would need preg_match function because i would need to print out the strings from multiple findstr commands. Ideas are welcome thanks!

    findstr /i /C:"User/Group Management : Success or Failure" policies.txt
    findstr /i /C:"Privileged Account Logon: Failure" policies.txt

    <?php
    $batch_file = 'audit_policy.bat';
    $batch_contents = file_get_contents($batch_file);
    
    
    
    if (preg_match("/Privileged Account Logon:/i", "$batch_contents")) {
        echo "prints specific string here";
    } else {
        echo "not found";
    }
    ?> 
    
    
    PHP:

     
    Last edited: Jul 20, 2010
    andytan91, Jul 20, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Your descriptions vague so can't work-out what your looking for, but assuming your wanting to retrieve everything between two markers (after : and before policies.txt) try this:

    <?php
    $batch_file = 'audit_policy.bat';
    $batch_contents = file_get_contents($batch_file);
    
    /* an attempt to make it generic */
    $starting_marker = "Privileged Account Logon";
    $ending_marker = "policies.txt";
    
    if (preg_match('~'.preg_quote($starting_marker).'\s*:(.+?)'.preg_quote($ending_marker).'~i', $batch_contents, $a)) {
        echo $a[1];
    } else {
        echo "not found";
    }
    ?>
    PHP:
     
    danx10, Jul 20, 2010 IP
  3. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    thanks a lot dude!
     
    Last edited: Jul 20, 2010
    andytan91, Jul 20, 2010 IP