开发者

Update PHP file constant from C#

开发者 https://www.devze.com 2023-03-08 17:53 出处:网络
I need to update a constant defi开发者_Go百科ned in a PHP file. The constants.php file is quite simple:

I need to update a constant defi开发者_Go百科ned 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 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:

  • 30 minute regex tutorial
  • Regex in C#
  • An example of using sub groups
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号