从 C# 更新 PHP 文件常量
我需要更新 PHP 文件中定义的常量。 constants.php
文件非常简单:
<?php
$firstConstant = "abcd";
$third = "abcd";
$updatedOn = "23 April 2001";
?>
现在我需要的是我的 C# 应用程序将此文件中的 $updatedOn
常量更新为当前日期。
我怎样才能做到这一点?提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个与
$updatedOn = "23 April 2001";
匹配的正则表达式,生成要放入文件中的替换行,然后使用String.Replace
方法将整个行替换为您创建的新行。下面是一个帮助您入门的正则表达式:\$updatedOn = \"([A-Za-z\W0-9]+)\"
对于更灵活的东西,您可以编写 简单解析器,它理解 PHP 的子集 - 即代码标签和赋值/字符串常量- 解析文件,将键/值对放入字典中,更新相关值,然后再次写回。
一些正则表达式资源:
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.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: