1

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!

Jim
  • 505
  • 3
  • 13
  • regular expression + replace? Why don't you use a config file? – erenon May 25 '11 at 12:10
  • That is what I need help with... I haven't used regular expressions before... And how do I use a config file? This file is used by other PHP pages to extract the values. – Jim May 25 '11 at 12:15

1 Answers1

2

You could write a regular expression which matches $updatedOn = "23 April 2001";, generate the replacement line to go in the file and then use the String.Replace method 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:

mdm
  • 12,480
  • 5
  • 34
  • 53
  • 2
    OMG! Write a parser! If this doesn't get me a raise, nothing else will! "Well, you see, I wrote a custom, dynamic parser that understands a _whole_ subset of PHP, including code tags and assignments/string constants, to make this work" ;) Thanks! – Jim May 25 '11 at 12:27
  • It's a great way to waste a few weekends :) – mdm May 25 '11 at 12:32