I need to update a constant defined in a PHP file. The constants.php file is quite simple:
<?php
$firstConstant = "abcd";
$third = "abcd";
$updatedOn = "23 April 2001";
?>
Now what I need is for my C# application to update the $updatedOn constant in this file to the present date.
How can I accomplish this? Thanks in advance!
You could write a regular expression which matches
$updatedOn = "23 April 2001";, generate the replacement line to go in the file and then use theString.Replacemethod to replace your entire line with the new one you have created. Here’s a regex to get you started:\$updatedOn = \"([A-Za-z\W0-9]+)\"For something a bit more flexible, you could write a simple parser which understands a subset of PHP – i.e. code tags and assignments/string constants – parse the file, put the key/value pairs into a dictionary, update the relevant values, and write it back out again.
Some Regular Expression resources: