preg_replace问题
我想输入一个文本而不是字符串 VERSION=20101203,我的问题只是 preg_replace 的第一个字段,我是正则表达式的新手。我不知道如何告诉 preg_replace 我需要将字符串 VERSION=20101203
更改为其他文本。
所以字符串的格式为:VERSION=YEARMONTHDAY
我正在尝试使用:
$new_content = preg_replace('/^VERSION\=[0-9]{8}/', $replacement, $content);
其中:
$replacement 是我想要的新字符串 $content 是文件的内容,这里并不重要,
我相信这不是太困难。您对此问题有任何疑问,请询问我,我会尽快答复,
非常感谢您
I would like to put a text instead of the string VERSION=20101203, my problem is only the first field of preg_replace, I am new to regular expresions. I don't know exactly how to tell preg_replace to see that I need to change the string VERSION=20101203
for other text.
So the string has the format:VERSION=YEARMONTHDAY
I was trying with:
$new_content = preg_replace('/^VERSION\=[0-9]{8}/', $replacement, $content);
Where:
$replacement is the new string I want to have
$content is the content of a file that it doesn't matter here
I beleive it's not too difficult. Any questions you may have with this issue please ask me and I will answer quickly
Thank you very much in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
^
将正则表达式仅锚定到行的开头。我认为这是问题的一部分。此外,您需要确保
$replacement
包含完整字符串,用于替换VERSION\=[0-9]{8}< 匹配的字符串/代码>。
^
is anchoring the regular expression to only the beginning of the line. I assume that's part of the problem.In addition, you will want to ensure that
$replacement
contains the full string for replacing the string matched byVERSION\=[0-9]{8}
.尝试以下代码(在正则表达式开头不带
^
):OUTPUT
Try this code (without
^
at the start of regular expression):OUTPUT
好吧,根据 Jason McCreary 的建议解决了。它只在没有 ^ 的情况下工作,其余的代码与我之前的代码相同。
我试图更改字符串 VERSION=YEARMONTHDAY (位于 $ks 文件的其中一行中)。我的意思是该文件的其中一行包含以下内容:
VERSION=20101203(或任何其他日期,但每次都使用相同的格式)
该字符串将被与变量 $ks 中存储的文件的最后一次修改相匹配的新字符串更改。 ($ks 是文件的名称)
因此最终结果将是:名为 $ks 的文件将包含 VERSION=20110622 的行,而不是 VERSION=20101203 (或任何其他较旧的日期)字符串。
该代码对我来说工作得很好。再次感谢大家,我不知道是否必须关闭这个问题,因为它已经解决了
PD:对不起我的英语
Well it was solved with Jason McCreary's suggestion. It worked just without ^ and the rest of the code is the same as I had it before.
I was trying to change the string VERSION=YEARMONTHDAY (which is in one of the lines of the $ks file). I mean that the file contains in one of its lines this:
VERSION=20101203 (or any other date, but everytime with the same format)
That string is going to be changed by a new one that matches to the last modification of the file stored in the variable $ks. ($ks is the name of the file)
So the final result is going to be: the file named $ks will have a line with VERSION=20110622 instead of the VERSION=20101203 (or any other older date) string.
The code is working fine this way for me. Thank you all again, I don't know if I have to close this issue, as it is solved
PD: Sorry for my english