我的 xml 中出现了以下文本几次:
Copyright © 2015.
我正在尝试使用正则表达式动态更改上述文本,以便始终在最终输出中反射(reflect)当前年份。这是我的尝试,但似乎没有用:
$finaloutput = preg_replace("/Copyright © [0-9]+/", "test", $finaloutput);
当然,我使用硬编码字符串(“测试”)只是为了测试目的,因为当前文本中已经包含 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 .这是我在各个输入字段中输入的用于测试的信息:
$模式:'/(Copyright\s+©\s+)[0-9]+/u'
$替换:'\1test'
$主题:<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', '\1test', $finaloutput);
关于PHP preg_replace() 改变版权年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34335647/