I have a php file. And in the php file, I have such a statement:
"$limit_value = ..."
"$limit_value = 5"
"$limit_value = 500"
"$limit_value = 34"
Assuming you have a
index.php and it looks something like this:
<?php $limit_value = 0; echo $limit_value; ?>
You could in python do:
import re rep = re.compile('\$limit_value =.*;') with open('/path/to/file/index.php', 'r+') as php: data = rep.sub('$limit_value = 5', php.read()) php.seek(0) # Places the text "Input" in the file at the beginning of the file php.write(data) php.truncate() # This will cause all trailing data to be flushed. # reason for doing this is because your default value of `$limit_value` might # make your total content written back to the file shorter of that was writter before. # there for there might be trailing data we need to truncate away.
This will create a regexp object that will find anything named
$limit_value =<anything>; and replace it with
$limit_value = 5; assuming 5 is the default value you want to use.