andytan91 Posted July 19, 2010 Share Posted July 19, 2010 Hello guys i am new to regular expression and i am figuring out how to give users a choice to change the argument in [0-90]. For example, user can change [0-90] to [5-60]... Batch file Script findstr /R /C:"Maximumpasswordage(days):[0-90]" final.doc if %errorlevel%==0 ( echo Maximum_Password_Age_Requirement:Pass >> C:\final.doc ) else ( echo Maximum_Password_Age_Requirement:Fail >> C:\final.doc ) $batch_contents = preg_replace('', 'findstr /R /C:"'.$newArgument.'"', $batch_contents); Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/ Share on other sites More sharing options...
.josh Posted July 19, 2010 Share Posted July 19, 2010 $newArgument="5-60"; $batch_contents = preg_replace('~(Maximumpasswordage\(days\):\[)([^\]]+)~','${1}'.$newArgument,$batch_contents); Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088120 Share on other sites More sharing options...
andytan91 Posted July 19, 2010 Author Share Posted July 19, 2010 thanks a lot it works! Can you kindly explain to me what does \[)([^\]]+)~','${1}' do? Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088135 Share on other sites More sharing options...
.josh Posted July 19, 2010 Share Posted July 19, 2010 findstr /R /C:"Maximumpasswordage(days):[0-90]" final.doc (Maximumpasswordage\(days\):\[)([^\]]+) The part of the string not matched in preg_replace Capture what is matched Match some literal text to isolate what you are wanting to replace. couple of backslashes thrown in there to match literally because parenthesis and square brackets are special characters in regex Negative character class: match 1 or more character that is not a closing square bracket. This matches your x-y between the opening and closing brackets. '${1}'.$newArgument $1 a temporary variable containing the first captured group of the pattern (the red stuff above). This is captured because a preg_replace replaces everything matched in the pattern. So you need to capture the stuff you are matching but not actually replacing, so that you can put it into the replacement so you don't lose it. The {} wrapped around the 1 is to prevent ambiguity. if $newArgument is "5-60", what happens normally is preg_replace tries to parse "$15-60" so it tries to use $15 (which doesn't exist) instead of $1. So the {} is used to tell preg_replace that you want $1 not $15. Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088144 Share on other sites More sharing options...
andytan91 Posted July 19, 2010 Author Share Posted July 19, 2010 wow thanks man! That was detailed, helped me alot Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088189 Share on other sites More sharing options...
andytan91 Posted July 20, 2010 Author Share Posted July 20, 2010 Hmm i would like users to to able to change text inside "Success or Failure" while retaining the whitespace between : and S. findstr /i /C:"Privileged Account Logon: Success or Failure" I have tried this regex but i still cant make it to work... preg_replace('~(Privileged Account Logon:)(\s[a-z]+)~','${1}'.$newArgument, $batch_contents) Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088481 Share on other sites More sharing options...
andytan91 Posted July 20, 2010 Author Share Posted July 20, 2010 okay i have solved it...but i have another question... whenever users input nothing into a form and clicks update, my regex replace function would stop working because my argument would be blank. I have put if(isset($_POST['submit']) && $_POST['submit'] == "Update") but it php still reads from the form.. Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088552 Share on other sites More sharing options...
.josh Posted July 20, 2010 Share Posted July 20, 2010 post your code Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088572 Share on other sites More sharing options...
andytan91 Posted July 20, 2010 Author Share Posted July 20, 2010 Alright so basically if user inputs nothing in the text field..my code will cock up and change into systeminfo | findstr /i /C:"" . If i try to input some text into it, it will still appear as blank... Batch file @echo off rem systeminfo | findstr OS | findstr -v BIOS >> hi.txt rem findstr /C:"OS Version: 5.1.2600 Service Pack 2 Build 2600" final.doc rem cd C:\UnxUtils\usr\local\wbin rem systeminfo | grep -w "\(OS Name\|OS Version\)" >> final.doc systeminfo | findstr /i /C:"OS Version" | findstr -v BIOS >> C:\final.doc systeminfo | findstr /i /C:"Service pack 2" if %errorlevel%==0 ( echo Service_Pack_Requirement:Pass >> C:\final.doc ) else ( echo Service_Pack_Requirement:Fail >> C:\final.doc ) Replace string php file <?php $batch_file = 'service_pack.bat'; $batch_contents = file_get_contents($batch_file); if(isset($_POST['submit']) && $_POST['submit'] == "Update") { $newArgument = $_POST['argument']; $batch_contents = preg_replace('~findstr /i /C:"([a-z0-9_ -]+)"~i', 'findstr /i /C:"'.$newArgument.'"', $batch_contents); file_put_contents($batch_file, $batch_contents); } ?> <p> <form action="replaceservicepack.php" method="post"> Change argument: <input type="text" name="argument"<br> <input type="submit" name="submit" value="Update"> <input type="button" value="Cancel" onclick="history.go(-1);"> </form> </p> Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1088609 Share on other sites More sharing options...
andytan91 Posted July 21, 2010 Author Share Posted July 21, 2010 never mind about the above, i managed to work around it:) I would like to replace the string in this line -> $batch_file1 = 'service_pack.bat'; It is located on another php file. I have tried doing it for an hour but to no avail, can someone guide me? Thanks! <?php $file = 'C:\xampp\htdocs\new\editpolicy.php'; $batch_contents = file_get_contents($file); if(isset($_POST['argument']) && $_POST['submit'] == "Save") { $newArgument = $_POST['argument']; preg_replace('~$\batch_file1 = "([a-z0-9_ -]+)"~i', '$\batch file1 = "'.$newArgument.'"' ,$batch_contents); }file_put_contents($file, $batch_contents); ?> <b>Deploy Service Pack Policy</b> <p> <form action="deploy.php" method="post"> Select audit file to deploy: <input type="text" name="argument"<br> <input type="submit" name="submit" value="Save"> <p> </form> </p> Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1089033 Share on other sites More sharing options...
.josh Posted July 21, 2010 Share Posted July 21, 2010 $batch_contents = preg_replace('~(\$batch_file1 = ")[^"]*~i', '${1}'.$newArgument,$batch_contents); Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1089092 Share on other sites More sharing options...
andytan91 Posted July 23, 2010 Author Share Posted July 23, 2010 thanks man..but i have another question.. Right now i am doing an edit policy function which uses the power of regular expression(preg_replace). But i have a problem here. The below is the text output from the DOS command whereby i will need to edit the START_TYPE of the following services. My question is how to make preg_replace to differentiate the START_TYPE that belongs to each service?? or do i have to separate each service to its own text file instead of combining all the services? START_TYPE:DISABLED DISPLAY_NAME:Alerter START_TYPE:AUTO_START DISPLAY_NAME:AutomaticUpdates START_TYPE:DEMAND_START DISPLAY_NAME:BackgroundIntelligentTransferService START_TYPE:DISABLED DISPLAY_NAME:ClipBook Quote Link to comment https://forums.phpfreaks.com/topic/208155-php-replace-string-in-batch-file/#findComment-1090139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.