PHP preg_replace() 更改版权年份

PHP preg_replace() to change copyright year

我的 xml 中出现了以下文本几次:

Copyright © 2015.

我正在尝试使用正则表达式动态更改上述文本,以便始终在最终输出中反映当前年份。这是我的尝试,但似乎没有用:

$finaloutput = preg_replace("/Copyright © [0-9]+/", "test", $finaloutput);

当然,我使用硬编码字符串 ("test") 只是为了测试目的,因为当前文本中已经包含 2015 年。所以,我的目标是改变这个:

Copyright © 2015. All rights reserved....

进入这个:

Copyright © test. All rights reserved....

我的正则表达式错了吗?

P.S.: 只是为了让大家更好地了解上下文,这里(的一部分)XML 片段 $finaloutput 分配给:

<div class="dict_source"><strong>Powered by</strong> <a target="_blank" rel="nofollow" href="http://www.collinsdictionary.com/dictionary/english-spanish">Collins Complete Spanish Electronic Dictionary</a><br>Copyright © 2015 HarperCollins Publishers Limited.</div>

更新:我尝试了@Avinash 提供的答案,但仍然无法正常工作。为了更好地说明这个问题,我挖了一个在线preg_replace()测试器。 https://www.functions-online.com/preg_replace.html 结束。这是我在各个输入字段中输入的用于测试的信息:

$pattern: '/(Copyright\s+©\s+)[0-9]+/u'

$替换: 'test'

$subject: <div class="dict_source"><strong>Powered by</strong> <a target="_blank" rel="nofollow" href="http://www.collinsdictionary.com/dictionary/english-spanish">Collins Complete Spanish Electronic Dictionary</a><br>Copyright © 2015 HarperCollins Publishers Limited.</div>

在处理 unicode 字符时添加 u 修饰符。

$finaloutput = preg_replace('/(Copyright\s+©\s+)[0-9]+/u', 'test', $finaloutput);

在 PHP 中使用 Avinashs 正则表达式:

<?php
$subject    = '<div class="dict_source"><strong>Powered by</strong> <a target="_blank" rel="nofollow" href="http://www.collinsdictionary.com/dictionary/english-spanish">Collins Complete Spanish Electronic Dictionary</a><br>Copyright © 2015 HarperCollins Publishers Limited.</div>';
$regex      = '/(Copyright\s+©\s+)[0-9]+/u';
$output = preg_replace($regex, "${1}2016", $subject);
echo $output; // Happy new Year 2016!
?>

注意替换部分:016(正如人们通常认为的那样)让引擎感到困惑(应该使用哪个捕获组?</code>?<code>016?[虽然这个不存在]),所以解决方法是使用 curly 括号。