preg_replace问题

发布于 2024-11-16 17:46:55 字数 441 浏览 2 评论 0原文

我想输入一个文本而不是字符串 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-11-23 17:46:55

^ 将正则表达式仅锚定到行的开头。我认为这是问题的一部分。

$new_content = preg_replace('/VERSION\=[0-9]{8}/', $replacement, $content);

此外,您需要确保 $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.

$new_content = preg_replace('/VERSION\=[0-9]{8}/', $replacement, $content);

In addition, you will want to ensure that $replacement contains the full string for replacing the string matched by VERSION\=[0-9]{8}.

屌丝范 2024-11-23 17:46:55

尝试以下代码(在正则表达式开头不带 ^):

$content='foo VERSION=20101203 foo bar';
var_dump( preg_replace('/VERSION=[0-9]{8}/', 'replacement', $content));

OUTPUT

string(23) "foo replacement foo bar"

Try this code (without ^ at the start of regular expression):

$content='foo VERSION=20101203 foo bar';
var_dump( preg_replace('/VERSION=[0-9]{8}/', 'replacement', $content));

OUTPUT

string(23) "foo replacement foo bar"
梦里人 2024-11-23 17:46:55

好吧,根据 Jason McCreary 的建议解决了。它只在没有 ^ 的情况下工作,其余的代码与我之前的代码相同。

我试图更改字符串 VERSION=YEARMONTHDAY (位于 $ks 文件的其中一行中)。我的意思是该文件的其中一行包含以下内容:
VERSION=20101203(或任何其他日期,但每次都使用相同的格式)

该字符串将被与变量 $ks 中存储的文件的最后一次修改相匹配的新字符串更改。 ($ks 是文件的名称)

    $last_modification = filemtime($ks);
    $last_modification = date("Ymd", $last_modification);

    // $last_modification (for instance suppose it is VERSION=20110622)
    $last_modification="VERSION=" . $last_modification;

    // Open the file in order to change the string
    $file = $ks;
    $fh = fopen($ks, 'r+');
    $content = fread($fh, filesize($ks));

    $new_content = preg_replace('/VERSION\=[0-9]{8}/', $last_modification, $content);
    fclose($fh);

    // Open the file in order to write inside it
    $fh = fopen($ks, 'r+');
    fwrite($fh, $new_content);
    fclose($fh); 

因此最终结果将是:名为 $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)

    $last_modification = filemtime($ks);
    $last_modification = date("Ymd", $last_modification);

    // $last_modification (for instance suppose it is VERSION=20110622)
    $last_modification="VERSION=" . $last_modification;

    // Open the file in order to change the string
    $file = $ks;
    $fh = fopen($ks, 'r+');
    $content = fread($fh, filesize($ks));

    $new_content = preg_replace('/VERSION\=[0-9]{8}/', $last_modification, $content);
    fclose($fh);

    // Open the file in order to write inside it
    $fh = fopen($ks, 'r+');
    fwrite($fh, $new_content);
    fclose($fh); 

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文